【发布时间】:2018-11-06 14:25:31
【问题描述】:
我是机器学习的新手,对机器学习库几乎没有经验。我正在开发一个神经网络来预测给定输入数组的数字数组。所以我的输入如下:
神经网络的输入是一个长度为 100 的数组。
还有输出:
模型的输出是长度为 60 的数组。
到目前为止,我在这里发布了我的代码。它目前不工作。有人可以帮助我以正确的方式做到这一点吗?
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Flatten(input_shape=(100,)))
model.add(tf.keras.layers.Dense(768, activation=tf.nn.relu))
model.add(tf.keras.layers.Dense(768, activation=tf.nn.relu))
model.add(tf.keras.layers.Dense(60, activation=tf.nn.softmax))
model.compile(optimizer = 'adam',
loss = 'sparse_categorical_crossentropy',
metrices = ['accuracy'])
model.fit(x_train_new, y_train_new, epochs = 20)
注意:x_train_new 是一个长度为 100 的数组,y_train_new 是一个长度为 60 的数组。
当我运行它并调用model.fit() 时,会出现以下错误。
ValueError Traceback (most recent call last)
<ipython-input-62-33e8f1dfe4c8> in <module>()
5 # print(y_train_new.shape)
6
----> 7 model.fit(x_train_new, y_train_new, epochs = 20)
/home/vajira/tf_cpu_py3/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, **kwargs)
1276 steps_name='steps_per_epoch',
1277 steps=steps_per_epoch,
-> 1278 validation_split=validation_split)
1279
1280 # Prepare validation data.
/home/vajira/tf_cpu_py3/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, batch_size, check_steps, steps_name, steps, validation_split)
915 feed_output_shapes,
916 check_batch_axis=False, # Don't enforce the batch size.
--> 917 exception_prefix='target')
918
919 # Generate sample-wise weight values given the `sample_weight` and
/home/vajira/tf_cpu_py3/lib/python3.6/site-packages/tensorflow/python/keras/engine/training_utils.py in standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
189 'Error when checking ' + exception_prefix + ': expected ' +
190 names[i] + ' to have shape ' + str(shape) +
--> 191 ' but got array with shape ' + str(data_shape))
192 return data
193
ValueError: Error when checking target: expected dense_14 to have shape (1,) but got array with shape (60,)
【问题讨论】:
-
你能告诉我们更多为什么它不起作用吗?例如,它给出的错误将是一个好的开始......
-
@Alexis 错误发布
标签: python tensorflow machine-learning neural-network keras