0

I'm getting this error.

ValueError: Error when checking input: expected input_1 to have 3 dimensions, but got array with shape (860, 11)

These are the below codes that I'm using. The df has 860x15 dimension so datX has 860x11 dimension

# first neural network with keras tutorial
from numpy import loadtxt
from keras.models import Sequential
from keras.layers import Dense

import pandas as pd
from pandas import ExcelWriter
from pandas import ExcelFile

df =pd.read_excel('C:/Users/ASUS/Documents/Script/Simulation.Machine.V1/Final.xlsx', sheetname= "C0")
datX = df.drop(['C0', 'C1', 'C2', 'C3'], axis=1)

import numpy as np
datY = df['C1'] / df['C0'] 
datW = df['C0']**(1/2)
datZ = df['C1']

q=20
p= len(datX.columns)
from keras import backend as K

# define the keras model
model = Sequential()
model.add(Dense(q, input_dim=p, activation='tanh'))
model.add(Dense(1, activation= K.exp))

# define the keras model
offset = Sequential()
offset.add(Dense(1, input_dim=1, activation='linear'))

from keras.layers import Input
tweet_a = Input(shape=(860, 11))
tweet_b = Input(shape=(860, 1))
tweetx = model(tweet_a)
tweety = offset(tweet_b)

from keras.layers import Multiply, add
output = Multiply()([tweetx, tweety])

from keras.models import Model
modelnew = Model(inputs=[tweet_a, tweet_b], outputs=output)

modelnew.compile(optimizer='rmsprop',loss='mse',metrics=['accuracy'])

modelnew.fit([datX, datW], datY, epochs=100, batch_size=10000)

I expect the output would be a 1 dimension and have a 11 dimension input

1 Answer 1

1

It's pretty obvious what's going on here. In order to understand that, you will need to know the difference between the batch_shape and shape arguments in the Input layer.

When you specify the shape argument it actually adds a new dimension (i.e. batch dimension) to the beginning of the shape. Therefore, when you pass a 860x11 as shape, the actual model expects a bx860x11 sized output (where b is the batch size). What you have specified here is the value for the batch_shape argument. So there are two solutions.

The best solution for you would be to change the above to the following. Because this way you're not relying on a fixed batch dimension.

tweet_a = Input(shape=(11,))
tweet_b = Input(shape=(1,))
tweetx = model(tweet_a)
tweety = offset(tweet_b)

But if you're 100% sure the batch size is always 860 you can try the option below.

tweet_a = Input(batch_shape=(860, 11))
tweet_b = Input(batch_shape=(860, 1))
tweetx = model(tweet_a)
tweety = offset(tweet_b)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.