简体   繁体   中英

Understanding a Keras Error: TypeError: Value passed to parameter 'shape' has DataType float32 not in list of allowed values: int32, int64

So I have this line of code:

history = model.fit(X_train, y_train, batch_size=batch_size, epochs=epochs, verbose=1, validation_data=(X_val, y_val))

Which throws this error:

File "CNN.py", line 125, in model
    history = model.fit(X_train, y_train, batch_size=batch_size, epochs=epochs, verbose=1, validation_data=(X_val, y_val))
  File "C:\Users\Boche\AppData\Local\conda\conda\envs\ExerFloorTracking\lib\site-packages\keras\engine\training.py", line 952, in fit
    batch_size=batch_size)
  File "C:\Users\Boche\AppData\Local\conda\conda\envs\ExerFloorTracking\lib\site-packages\keras\engine\training.py", line 677, in _standardize_user_data
    self._set_inputs(x)
  File "C:\Users\Boche\AppData\Local\conda\conda\envs\ExerFloorTracking\lib\site-packages\keras\engine\training.py", line 589, in _set_inputs
    self.build(input_shape=(None,) + inputs.shape[1:])
  File "C:\Users\Boche\AppData\Local\conda\conda\envs\ExerFloorTracking\lib\site-packages\keras\engine\sequential.py", line 221, in build
    x = layer(x)
  File "C:\Users\Boche\AppData\Local\conda\conda\envs\ExerFloorTracking\lib\site-packages\keras\engine\base_layer.py", line 431, in __call__
    self.build(unpack_singleton(input_shapes))
  File "C:\Users\Boche\AppData\Local\conda\conda\envs\ExerFloorTracking\lib\site-packages\keras\layers\core.py", line 866, in build
    constraint=self.kernel_constraint)
  File "C:\Users\Boche\AppData\Local\conda\conda\envs\ExerFloorTracking\lib\site-packages\keras\legacy\interfaces.py", line 91, in wrapper
    return func(*args, **kwargs)
  File "C:\Users\Boche\AppData\Local\conda\conda\envs\ExerFloorTracking\lib\site-packages\keras\engine\base_layer.py", line 249, in add_weight
    weight = K.variable(initializer(shape),
  File "C:\Users\Boche\AppData\Local\conda\conda\envs\ExerFloorTracking\lib\site-packages\keras\initializers.py", line 218, in __call__
    dtype=dtype, seed=self.seed)
  File "C:\Users\Boche\AppData\Local\conda\conda\envs\ExerFloorTracking\lib\site-packages\keras\backend\tensorflow_backend.py", line 4139, in random_uniform
    dtype=dtype, seed=seed)
  File "C:\Users\Boche\AppData\Local\conda\conda\envs\ExerFloorTracking\lib\site-packages\tensorflow_core\python\ops\random_ops.py", line 245, in random_uniform
    rnd = gen_random_ops.random_uniform(shape, dtype, seed=seed1, seed2=seed2)
  File "C:\Users\Boche\AppData\Local\conda\conda\envs\ExerFloorTracking\lib\site-packages\tensorflow_core\python\ops\gen_random_ops.py", line 822, in random_uniform
    name=name)
  File "C:\Users\Boche\AppData\Local\conda\conda\envs\ExerFloorTracking\lib\site-packages\tensorflow_core\python\framework\op_def_library.py", line 632, in _apply_op_helper
    param_name=input_name)
  File "C:\Users\Boche\AppData\Local\conda\conda\envs\ExerFloorTracking\lib\site-packages\tensorflow_core\python\framework\op_def_library.py", line 61, in _SatisfiesTypeConstraint
    ", ".join(dtypes.as_dtype(x).name for x in allowed_list)))
TypeError: Value passed to parameter 'shape' has DataType float32 not in list of allowed values: int32, int64

shapes and types for training and validation data:

X training:
(28581, 46, 62, 1)
int32
y training:
(28581, 8)
int32
X validation:
(13720, 46, 62, 1)
int32
y validation:
(13720, 8) 

batch size is set to 100 and epochs is set to 20. I don't understand why the error is coming up. All values that need to be integers, are integers. I also don't understand what here is meant by the parameter "shape". If you don't see what is wrong in the code I would appreciate it if you could explain this error and what triggers it to me.

Edit: I forgott to add the line of code I'm talking about. I now added it to the post. It is the first line of code you see in the post.

So I solved the problem. It came from another line of code. These were the lines in my code that came before the fitting:

model.add(Dense(num_neurons, activation= cnn_params["activation_output"]))
model.add(Dense(cnn_params["final_dense"]["number_neurons"], activation= cnn_params["activation_output"]))

#COMPILING MODEL
model.compile(loss=keras.losses.categorical_crossentropy, optimizer=keras.optimizers.SGD(lr=learning_rate), metrics=['accuracy', 'categorical_accuracy'])

In the first line you can see the parameter num_neurons . I calculated this parameter using a funtion. The output of that funtion was a float. Casting it to an integer like this:

model.add(Dense(int(num_neurons), activation= cnn_params["activation_output"]))

solves the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM