为什么不让Keras接受我的意见?

时间:2017-03-11 18:03:37

标签: arrays python-2.7 numpy keras

为什么Keras中的模型不会输入我的输入/输出数据?

输入数据包含一个numpy.ndarrays形状列表(15,1,3),输出是numpy.arrays列表,每个条目只有一个数字。

这是我创建模型的地方,并传递内容:

        model = Sequential()

        print "Data-train-in: " + str(data_train_input[0].shape)
        print "Data-train-out: " + str(data_train_output[0].shape)
        print "Data-test-in: " + str(data_test_input[0].shape)
        #sys.exit()

        print "Model Definition"
        print "Row: " + str(row)
        model.add(Convolution2D(64,3,3,input_shape=(3,row,1)))
        print model.output_shape
        model.add(Convolution2D(32,1,3))
        print model.output_shape
        model.add(MaxPooling2D((1,1)))
        print model.output_shape
        model.add(Flatten())
        print model.output_shape
        model.add(Dense(1,activation='relu'))
        print model.output_shape


        model.compile(loss='mean_squared_error', optimizer="sgd")

        reduce_lr=ReduceLROnPlateau(monitor='val_loss', factor=0.01, patience=3, verbose=1, mode='auto', epsilon=0.0001, cooldown=0, min_lr=0.000000000000000001)
        stop  = EarlyStopping(monitor='val_loss', min_delta=0, patience=5, verbose=1, mode='auto')

        log=csv_logger = CSVLogger('training_'+str(i)+'.csv')
        print "Model Train"
        hist_current = model.fit(data_train_input,
                            data_train_output,
                            shuffle=False,
                            validation_data=(data_test_input,data_test_output),
                            validation_split=0.1,
                            nb_epoch=150,
                            verbose=1,
                            callbacks=[reduce_lr,log,stop])

哪个输出:

Data-train-in: (15, 1, 3)
Data-train-out: ()
Data-test-in: (15, 1, 3)
Model Definition
Row: 15
(None, 1, 13, 64)
(None, 1, 11, 32)
(None, 1, 11, 32)
(None, 352)
(None, 1)
Model Train
Traceback (most recent call last):
  File "keras_convolutional_feature_extraction.py", line 502, in <module>
    model(0,train_input_data,output_data_train,test_input_data,output_data_test)
  File "keras_convolutional_feature_extraction.py", line 496, in model
    callbacks=[reduce_lr,log,stop])
  File "/usr/local/lib/python2.7/dist-packages/keras/models.py", line 652, in fit
    sample_weight=sample_weight)
  File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 1038, in fit
    batch_size=batch_size)
  File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 963, in _standardize_user_data
    exception_prefix='model input')
  File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 54, in standardize_input_data
    '...')
Exception: Error when checking model input: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 1 arrays but instead got the following list of 260182 arrays: [array([[[ 67, 255, 180]],

       [[ 68, 255, 178]],

       [[ 68, 255, 178]],

       [[ 67, 255, 180]],

       [[ 43, 254, 204]],

       [[ 19, 253, 228]],

       [[  9, 205, 241]],

       [[ ...

我不确定如何解释输出消息。这有什么不对?

2 个答案:

答案 0 :(得分:1)

您的数据与输入图层不匹配。在您的模型中,您使用的input_shape=(3,row,1)在此上下文中等于input_shape=(3,15,1)。 但是您的打印显示您的训练示例具有不同的(15, 1, 3)形状。

尝试将输入定义更改为input_shape=(row,1,3)。 解决问题的另一种方法是将数据重新整形为输入图层形状。

答案 1 :(得分:1)

import numpy as np  
data_train_input = np.array(data_train_input)

这似乎有效。