Reputation: 599
This is a long question because i try to explain the more i can my problem because it's a recurrent problem for me and i really don't understand so thank you for taking the time to read me
I want to create a sequential dense model which takes as input list with dimension like this:
[batch_size, data_dimension]
So i defined my network like this:
ModelDense = Sequential()
ModelDense.add(Dense(380, input_shape=(None,185), activation='elu', kernel_initializer='glorot_normal'))
ModelDense.add(Dense(380, activation='elu', kernel_initializer='glorot_normal'))
ModelDense.add(Dense(380, activation='elu', kernel_initializer='glorot_normal'))
ModelDense.add(Dense(7, activation='elu', kernel_initializer='glorot_normal'))
optimizer = tf.keras.optimizers.Adam(lr=0.00025)
ModelDense.compile(loss='mean_squared_error', optimizer=optimizer, metrics=['accuracy'])
but when i use this network with input shaped like this : (1, 185) i got the error:
Error when checking input: expected dense_input to have 3 dimensions, but got array with shape (185, 1)
Don't ask me why i said that my vector shape is (1, 185) and in the error message we see (185, 1) because when i check my array shape just before giving it as input to my network the shape shown is (1, 185)
Ok, so i checked some topics then i found this one in which is explained that :
Dense layers require inputs as (batch_size, input_size) or (batch_size, optional,...,optional, input_size)
So that is what i did idn't it ? But i also saw that:
Shapes in Keras :
...
So, even if you used input_shape=(50,50,3), when keras sends you messages, or when you print the model summary, it will show (None,50,50,3)
...
So, when defining the input shape, you ignore the batch size: input_shape=(50,50,3)
Ok ! let's try i now definied my input layer like this :
ModelDense.add(Dense(380, input_shape=(185,), activation='elu', kernel_initializer='glorot_normal'))
When i do a model.summary() :
_________________________________________________________________ Layer (type) Output Shape Param # ================================================================= dense (Dense) (None, 380) 70680 _________________________________________________________________ dense_1 (Dense) (None, 380) 144780 _________________________________________________________________ dense_2 (Dense) (None, 380) 144780 _________________________________________________________________ dense_3 (Dense) (None, 7) 2667 ================================================================= Total params: 362,907 Trainable params: 362,907 Non-trainable params: 0
Ok i think it's that i want but when i give THE SAME array as input i now get the error:
ValueError: Error when checking input: expected dense_input to have shape (185,) but got array with shape (1,)
I'm confused, what am i misunderstanding ?
_________EDIT__________ :
Prediction function:
def predict(dense_model, state, action_size, epsilon):
alea = np.random.rand()
# DEBUG
print(state)
print(np.array(state).shape)
output = dense_model.predict(state)
if (epsilon > alea):
action = random.randint(1, action_size) - 1
flag_alea = True
else:
action = np.argmax(output)
flag_alea = False
return output, action, flag_alea
The line where i use my function:
Qs, action, flag_alea = predict(Dense_model, [state], ACTION_SIZE, Epsilon)
Exact result of my 'DEBUG' printing:
[[0.0, 0.0, 0.0, 0.12410027302060064, 0.0, 0.0, 0.0, 0.0, 0.0, 0.18851780241253108, 0.0, 0.0, 0.2863141820958198, 0.0, 0.07328154770628756, 0.418848167539267, 0.07328154770628756, 0.2094240837696335, 0.42857142857142855, 0.0, 0.12410027302060064, 0.0, 0.0, 0.0, 0.0, 0.263306220774655, 0.14740566037735847, 0.40346984062941293, 0.675310642895732, 0.0, 0.0, 0.0, 0.0, 0.07328154770628756, 0.0, 0.4396892862377253, 0.0, 0.42857142857142855, 0.0, 0.12410027302060064, 0.08759635599159075, 0.0, 0.1401927621025243, 0.6755559204272007, 0.0, 0.0, 0.11564568886156315, 0.4051863857374392, 0.0, 0.0, 0.19087612139721322, 0.0, 0.07328154770628756, 0.6282722513089005, 0.14656309541257512, 0.10471204188481675, 0.42857142857142855, 0.0, 0.12410027302060064, 0.0, 0.0, 0.0, 0.0, 0.0974621385076755, 0.0, 0.0, 0.675310642895732, 0.0, 0.0, 0.0, 0.09543806069860661, 0.07328154770628756, 0.10471204188481675, 0.5129708339440129, 0.5233396901920598, 0.42857142857142855, 0.0, 0.0, 0.0, 0.0, 0.5528187746700128, 0.6755564266434103, 0.0, 0.0, 0.10086746015735323, 0.1350621285791464, 0.0, 0.0, 0.0, 0.0, 0.14891426591693724, 0.5166404112353377, 0.14656309541257512, 0.10471204188481675, 0.42857142857142855, 0.00846344605088234, 0.012550643645226955, 0.0, 0.0, 0.004527776502072811, 0.0, 0.001294999849051237, 0.019391579553484917, 0.02999694086611271, 0.0026073455810546875, 0.0, 0.0, 0.016546493396162987, 0.024497902020812035, 0.00018889713101089, 0.0, 0.005568447522819042, 0.0, 0.007975691929459572, 0.01434263214468956, 0.0, 6.733229383826256e-05, 0.0012099052546545863, 0.0, 0.0001209513284265995, 0.01868056133389473, 0.025530844926834106, 0.004079729784280062, 0.0, 0.0, 0.01332627609372139, 0.026645798236131668, 0.0, 0.0, 0.007684763520956039, 0.0, 0.010554256848990917, 0.007236589677631855, 0.0013368092477321625, 0.000697580398991704, 0.00213554291985929, 0.0, 0.0021772112231701612, 0.012761476449668407, 0.015171871520578861, 0.001512336079031229, 0.0, 0.0, 0.008273545652627945, 0.01777557097375393, 0.006600575987249613, 0.0, 0.007174563594162464, 0.0, 0.004660750739276409, 0.009024208411574364, 0.0, 0.0014235835988074541, 0.0, 0.0, 0.0, 0.008785379119217396, 0.010602384805679321, 0.0024691042490303516, 0.0, 0.0, 0.003091508522629738, 0.0120345214381814, 0.003123666625469923, 0.0, 0.005664713680744171, 0.0, 0.004825159907341003, 0.0034197410568594933, 0.0030767947901040316, 0.004110954236239195, 0.0, 0.0, 0.001896441332064569, 0.002400417113676667, 0.0012791997287422419, 0.0, 0.0, 0.0, 0.0021027529146522284, 0.006922871805727482, 0.004868669901043177, 0.0, 7.310241926461458e-05, 0.0]]
(1, 185)
_________EDIT2__________ :
Error traceback:
File ".!Qltrain.py", line 360, in Qs, action, flag_alea = predict(Dense_model, [state], ACTION_SIZE, Epsilon) File ".\Lib\Core.py", line 336, in predict output = dense_model.predict(state) File "C:\Users\Odeven\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\keras\engine\training.py", line 1096, in predict x, check_steps=True, steps_name='steps', steps=steps) File "C:\Users\Odeven\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\keras\engine\training.py", line 2382, in _standardize_user_data exception_prefix='input') File "C:\Users\Odeven\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\keras\engine\training_utils.py", line 362, in standardize_input_data ' but got array with shape ' + str(data_shape)) ValueError: Error when checking input: expected dense_input to have shape (185,) but got array with shape (1,)
If u check out the first 3 lines you can see that the code from where the erorr is coming is the code i added in my first edit
_______self-containing example_______
Content of test.py:
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
import random
import numpy as np
ModelDense = Sequential()
ModelDense.add(Dense(380, input_shape=(185,), activation='elu', kernel_initializer='glorot_normal'))
ModelDense.add(Dense(380, activation='elu', kernel_initializer='glorot_normal'))
ModelDense.add(Dense(380, activation='elu', kernel_initializer='glorot_normal'))
ModelDense.add(Dense(7, activation='elu', kernel_initializer='glorot_normal'))
optimizer = tf.keras.optimizers.Adam(lr=0.00025)
ModelDense.compile(loss='mean_squared_error', optimizer=optimizer, metrics=['accuracy'])
ModelDense.summary()
def predict(dense_model, state, action_size, epsilon):
alea = np.random.rand()
print(state)
print(np.array(state).shape)
dense_model.summary()
output = dense_model.predict(state)
if (epsilon > alea):
action = random.randint(1, action_size) - 1
flag_alea = True
else:
action = np.argmax(output)
flag_alea = False
return output, action, flag_alea
state = []
state.append([np.random.rand()] * 185)
output, ac, flag = predict(ModelDense, state, 7, 0.0)
print(output)
Complete output:
_________________________________________________________________ Layer (type) Output Shape Param # ================================================================= dense (Dense) (None, 380) 70680 _________________________________________________________________ dense_1 (Dense) (None, 380) 144780 _________________________________________________________________ dense_2 (Dense) (None, 380) 144780 _________________________________________________________________ dense_3 (Dense) (None, 7) 2667 ================================================================= Total params: 362,907 Trainable params: 362,907 Non-trainable params: 0 _________________________________________________________________ [[0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739, 0.11966889292971739]] (1, 185) _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= dense (Dense) (None, 380) 70680 _________________________________________________________________ dense_1 (Dense) (None, 380) 144780 _________________________________________________________________ dense_2 (Dense) (None, 380) 144780 _________________________________________________________________ dense_3 (Dense) (None, 7) 2667 ================================================================= Total params: 362,907 Trainable params: 362,907 Non-trainable params: 0 _________________________________________________________________ Traceback (most recent call last): File ".\test.py", line 47, in output, ac, flag = predict(ModelDense, state, 7, 0.0) File ".\test.py", line 31, in predict output = dense_model.predict(state) File "C:\Users\Odeven\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\keras\engine\training.py", line 1096, in predict x, check_steps=True, steps_name='steps', steps=steps) File "C:\Users\Odeven\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\keras\engine\training.py", line 2382, in _standardize_user_data exception_prefix='input') File "C:\Users\Odeven\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\keras\engine\training_utils.py", line 362, in standardize_input_data ' but got array with shape ' + str(data_shape)) ValueError: Error when checking input: expected dense_input to have shape (185,) but got array with shape (1,)
Upvotes: 1
Views: 289
Reputation: 65
I am not a pro of Keras but I think, as @Matias Valdenegro said and since the algorithm is going to perform matrix multiplications through the network, it is expecting you to give an array.
If you have only one state on which you want to perform prediction you can expand the dim of your data as follow :
state = np.expand_dims(state, axis=0)
Upvotes: 0
Reputation: 56357
Change this:
output = dense_model.predict(state)
Into this:
output = dense_model.predict(np.array(state))
Seems keras gets confused if you pass a plain list to predict and might not do what you want, this way you make sure that state
is a numpy array of the shape you expect.
Upvotes: 2