繁体   English   中英

张量输入形状与模型拟合的问题

[英]Problem with tensor input shape wile fitting a model

我正在运行一个简单的神经网络接口。

model = tf.keras.models.Sequential([
  #tf.keras.layers.Flatten(input_shape=(10, 1)),
  tf.keras.layers.Dense(128, activation='relu'),
  tf.keras.layers.Dropout(0.2),
  tf.keras.layers.Dense(2, activation='softmax')
])

给定损失函数:

          model.compile(optimizer='adam',
          loss='categorical_crossentropy',
          metrics=['accuracy'])

输入形状为 [10,1] 的张量,这意味着值和标签。 当我拟合模型时,出现错误:

output_shape = [product, shape[-1]] IndexError:列表索引超出范围

我正在使用 tensorflow 2.0

这是输出错误:

If using Keras pass *_constraint arguments to layers.
Train on 200000 steps
Epoch 1/5
2020-02-02 12:48:13.088278: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcublas.so.10.0
Traceback (most recent call last):
  File "MLP.py", line 121, in <module>
    model.fit(train, epochs=5)
  File "/home/jacek/anaconda3/envs/alice_tf/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training.py", line 727, in fit
    use_multiprocessing=use_multiprocessing)
  File "/home/jacek/anaconda3/envs/alice_tf/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training_arrays.py", line 675, in fit
    steps_name='steps_per_epoch')
  File "/home/jacek/anaconda3/envs/alice_tf/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training_arrays.py", line 300, in model_iteration
    batch_outs = f(actual_inputs)
  File "/home/jacek/anaconda3/envs/alice_tf/lib/python3.7/site-packages/tensorflow_core/python/keras/backend.py", line 3476, in __call__
    run_metadata=self.run_metadata)
  File "/home/jacek/anaconda3/envs/alice_tf/lib/python3.7/site-packages/tensorflow_core/python/client/session.py", line 1472, in __call__
    run_metadata_ptr)
tensorflow.python.framework.errors_impl.InvalidArgumentError: 2 root error(s) found.
  (0) Invalid argument: Expected dimension in the range [0, 0), but got -1
     [[{{node metrics/acc/ArgMax}}]]
     [[loss/mul/_59]]
  (1) Invalid argument: Expected dimension in the range [0, 0), but got -1
     [[{{node metrics/acc/ArgMax}}]]
0 successful operations.
0 derived errors ignored.

你能提供model.fit调用吗? 这是一组工作代码。 工作,这意味着它不会使用 tensorflow 2.0 或 2.1 引发错误。

import numpy as np
import tensorflow as tf

model = tf.keras.models.Sequential([
  #tf.keras.layers.Flatten(input_shape=(10, 1)),
  tf.keras.layers.Dense(128, activation='relu'),
  tf.keras.layers.Dropout(0.2),
  tf.keras.layers.Dense(2, activation='softmax')
])

model.compile(
    optimizer='adam',
    loss='categorical_crossentropy',
    metrics=['accuracy'])

# Generate fake data.
x = np.ones([10, 1], dtype=np.float32)
y = np.ones([10, 2], dtype=np.float32)

# Train the model, providing features and labels.
model.fit(x, y)

您得到的错误可能是因为您没有向model.fit提供y

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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