简体   繁体   中英

model.fit() generates ERROR: ValueError: All input arrays (x) should have the same number of samples

I have a keras model with several custom layers. When I run:

model_.compile(optimizer=rms, loss=contrastive_loss,metrics=['accuracy'])

It compiles without any problems. But when I try to fit the model with a list of arrays:

X = [T1,R1] + [T2, R2]
model_.fit(X, [None]*2, epochs=50, batch_size=32)

I get an error. It seems that it is caused from engine\\training.pyc, as it prints:

C:\Tools\Anaconda2\lib\site-packages\keras\engine\training.pyc in _standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
124     for i in range(len(names)):
125         array = arrays[i]
126         if len(array.shape) == 1:
127             array = np.expand_dims(array, 1)
128             arrays[i] = array

AttributeError: 'NoneType' object has no attribute 'shape'

Can maybe anyone help? I am using keras 2.1.2 with theano 0.9.0

EDIT:

I tried :

model_.fit(X, [np.asarray([None])]*2, epochs=50, batch_size=32, verbose=5) 

instead and now I get the following error:

ValueError: All input arrays (x) should have the same number of samples. Got array shapes: 

and then it prints the shapes of my input arrays.

any idea ?

I think it returns an error because Keras is checking the shapes of your inputs. However lists (what you are feeding) don't have a shape attribute. Try passing them as arrays:

import numpy as np
model_.fit(np.asarray(X), np.asarray([None]*2), epochs=50, batch_size=32)

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