Tensorflow ValueError 形状不兼容

时间:2021-02-27 18:55:32

标签: python python-3.x tensorflow keras neural-network

无论我做什么,我都无法解决这个 ValueError 出现的问题:ValueError: Shapes (35, 1) and (700, 35) is incompatible 我是 tensorflow 的新手,正在尝试构建一个“简单”的,可能仍然有点大的神经网络。我曾尝试更改 input_shape、损失函数和神经元数量,但没有成功。

我已经包含了我认为重要的代码部分,其余的只是获取数据并对其进行格式化。


model = tf.keras.Sequential([
    #tf.keras.layers.Flatten(input_shape=700,),
    tf.keras.layers.Dense(800, activation='sigmoid', input_shape=(700,)),
    tf.keras.layers.Dense(1000, activation='sigmoid'),
    tf.keras.layers.Dense(500, activation='sigmoid'),
    tf.keras.layers.Dense(150, activation='sigmoid'),
    tf.keras.layers.Dense(35)
])


model.compile(optimizer='adam',
              loss=tf.keras.losses.sparse_categorical_crossentropy,
              metrics=['accuracy'])

train_dataset = tf.data.Dataset.from_tensor_slices((arrayInput, arrayTarget))

for feat, targ in train_dataset.take(5):
  print('Features: {}, Target: {}'.format(feat, targ))

model.fit(train_dataset, epochs=EPOCHS)


model.save('savedmodel')

输出:

Features: [8.32999992e+00 8.18400002e+00 8.10999966e+00 8.05000019e+00
 ...SHORTENED BUT 700 LONG...
 1.13643000e+05 7.27480000e+04 1.00100000e+05 3.49750000e+04], Target: [8.72999954 8.75       8.64099979 8.60000038 8.64000034 8.66499996
 8.52999973 8.51000023 8.52000046 8.56000042 8.51000023 8.95499992
 8.85999966 8.75010014 8.74499989 8.75       8.76000023 8.77000046
 8.64500046 8.65200043 8.60429955 8.69999981 8.89000034 8.97999954
 8.92000008 9.21000004 9.38000011 9.47599983 9.57999992 9.46500015
 9.44999981 9.57999992 9.625      9.76000023 9.67000008]
Features: [8.18400002e+00 8.10999966e+00 8.05000019e+00 8.10999966e+00
 ...SHORTENED BUT 700 LONG...
 7.27480000e+04 1.00100000e+05 3.49750000e+04 3.91450000e+04], Target: [8.75       8.64099979 8.60000038 8.64000034 8.66499996 8.52999973
 8.51000023 8.52000046 8.56000042 8.51000023 8.95499992 8.85999966
 8.75010014 8.74499989 8.75       8.76000023 8.77000046 8.64500046
 8.65200043 8.60429955 8.69999981 8.89000034 8.97999954 8.92000008
 9.21000004 9.38000011 9.47599983 9.57999992 9.46500015 9.44999981
 9.57999992 9.625      9.76000023 9.67000008 9.64000034]
Features: [8.10999966e+00 8.05000019e+00 8.10999966e+00 8.13199997e+00
 ...SHORTENED BUT 700 LONG...
 1.00100000e+05 3.49750000e+04 3.91450000e+04 6.92160000e+04], Target: [8.64099979 8.60000038 8.64000034 8.66499996 8.52999973 8.51000023
 8.52000046 8.56000042 8.51000023 8.95499992 8.85999966 8.75010014
 8.74499989 8.75       8.76000023 8.77000046 8.64500046 8.65200043
 8.60429955 8.69999981 8.89000034 8.97999954 8.92000008 9.21000004
 9.38000011 9.47599983 9.57999992 9.46500015 9.44999981 9.57999992
 9.625      9.76000023 9.67000008 9.64000034 9.56499958]
Features: [8.05000019e+00 8.10999966e+00 8.13199997e+00 8.11999989e+00
 ...SHORTENED BUT 700 LONG...
 9.76000023 9.67000008 9.64000034 9.56499958 9.60999966], Target: [8.60000038 8.64000034 8.66499996 
 8.52999973 8.51000023 8.52000046
 8.56000042 8.51000023 8.95499992 8.85999966 8.75010014 8.74499989
 8.75       8.76000023 8.77000046 8.64500046 8.65200043 8.60429955
 8.69999981 8.89000034 8.97999954 8.92000008 9.21000004 9.38000011
 9.47599983 9.57999992 9.46500015 9.44999981 9.57999992 9.625
 9.76000023 9.67000008 9.64000034 9.56499958 9.60999966]
Features: [8.10999966e+00 8.13199997e+00 8.11999989e+00 8.06999969e+00
 ...SHORTENED BUT 700 LONG...
 3.91450000e+04 6.92160000e+04 9.24410000e+04 1.06220000e+05], Target: [8.64000034 8.66499996 8.52999973 8.51000023 8.52000046 8.56000042
 8.51000023 8.95499992 8.85999966 8.75010014 8.74499989 8.75
 8.76000023 8.77000046 8.64500046 8.65200043 8.60429955 8.69999981
 8.89000034 8.97999954 8.92000008 9.21000004 9.38000011 9.47599983
 9.57999992 9.46500015 9.44999981 9.57999992 9.625      9.76000023
 9.67000008 9.64000034 9.56499958 9.60999966 9.63000011]
Epoch 1/5
Traceback (most recent call last):
  File "C:/Users/Technik/PycharmProjects/StockNNv1/Train.py", line 88, in <module>
    model.fit(train_dataset, epochs=EPOCHS)
  File "C:\Users\Technik\PycharmProjects\StockNNv1\venv\lib\site-packages\tensorflow\python\keras\engine\training.py", line 1100, in fit
    tmp_logs = self.train_function(iterator)
  File "C:\Users\Technik\PycharmProjects\StockNNv1\venv\lib\site-packages\tensorflow\python\eager\def_function.py", line 828, in __call__
    result = self._call(*args, **kwds)
  File "C:\Users\Technik\PycharmProjects\StockNNv1\venv\lib\site-packages\tensorflow\python\eager\def_function.py", line 871, in _call
    self._initialize(args, kwds, add_initializers_to=initializers)
  File "C:\Users\Technik\PycharmProjects\StockNNv1\venv\lib\site-packages\tensorflow\python\eager\def_function.py", line 725, in _initialize
    self._stateful_fn._get_concrete_function_internal_garbage_collected(  # pylint: disable=protected-access
  File "C:\Users\Technik\PycharmProjects\StockNNv1\venv\lib\site-packages\tensorflow\python\eager\function.py", line 2969, in _get_concrete_function_internal_garbage_collected
    graph_function, _ = self._maybe_define_function(args, kwargs)
  File "C:\Users\Technik\PycharmProjects\StockNNv1\venv\lib\site-packages\tensorflow\python\eager\function.py", line 3361, in _maybe_define_function
    graph_function = self._create_graph_function(args, kwargs)
  File "C:\Users\Technik\PycharmProjects\StockNNv1\venv\lib\site-packages\tensorflow\python\eager\function.py", line 3196, in _create_graph_function
    func_graph_module.func_graph_from_py_func(
  File "C:\Users\Technik\PycharmProjects\StockNNv1\venv\lib\site-packages\tensorflow\python\framework\func_graph.py", line 990, in func_graph_from_py_func
    func_outputs = python_func(*func_args, **func_kwargs)
  File "C:\Users\Technik\PycharmProjects\StockNNv1\venv\lib\site-packages\tensorflow\python\eager\def_function.py", line 634, in wrapped_fn
    out = weak_wrapped_fn().__wrapped__(*args, **kwds)
  File "C:\Users\Technik\PycharmProjects\StockNNv1\venv\lib\site-packages\tensorflow\python\framework\func_graph.py", line 977, in wrapper
    raise e.ag_error_metadata.to_exception(e)
ValueError: in user code:

    C:\Users\Technik\PycharmProjects\StockNNv1\venv\lib\site-packages\tensorflow\python\keras\engine\training.py:805 train_function  *
        return step_function(self, iterator)
    C:\Users\Technik\PycharmProjects\StockNNv1\venv\lib\site-packages\tensorflow\python\keras\engine\training.py:795 step_function  **
        outputs = model.distribute_strategy.run(run_step, args=(data,))
    C:\Users\Technik\PycharmProjects\StockNNv1\venv\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:1259 run
        return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
    C:\Users\Technik\PycharmProjects\StockNNv1\venv\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:2730 call_for_each_replica
        return self._call_for_each_replica(fn, args, kwargs)
    C:\Users\Technik\PycharmProjects\StockNNv1\venv\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:3417 _call_for_each_replica
        return fn(*args, **kwargs)
    C:\Users\Technik\PycharmProjects\StockNNv1\venv\lib\site-packages\tensorflow\python\keras\engine\training.py:788 run_step  **
        outputs = model.train_step(data)
    C:\Users\Technik\PycharmProjects\StockNNv1\venv\lib\site-packages\tensorflow\python\keras\engine\training.py:755 train_step
        loss = self.compiled_loss(
    C:\Users\Technik\PycharmProjects\StockNNv1\venv\lib\site-packages\tensorflow\python\keras\engine\compile_utils.py:203 __call__
        loss_value = loss_obj(y_t, y_p, sample_weight=sw)
    C:\Users\Technik\PycharmProjects\StockNNv1\venv\lib\site-packages\tensorflow\python\keras\losses.py:152 __call__
        losses = call_fn(y_true, y_pred)
    C:\Users\Technik\PycharmProjects\StockNNv1\venv\lib\site-packages\tensorflow\python\keras\losses.py:256 call  **
        return ag_fn(y_true, y_pred, **self._fn_kwargs)
    C:\Users\Technik\PycharmProjects\StockNNv1\venv\lib\site-packages\tensorflow\python\util\dispatch.py:201 wrapper
        return target(*args, **kwargs)
    C:\Users\Technik\PycharmProjects\StockNNv1\venv\lib\site-packages\tensorflow\python\keras\losses.py:1537 categorical_crossentropy
        return K.categorical_crossentropy(y_true, y_pred, from_logits=from_logits)
    C:\Users\Technik\PycharmProjects\StockNNv1\venv\lib\site-packages\tensorflow\python\util\dispatch.py:201 wrapper
        return target(*args, **kwargs)
    C:\Users\Technik\PycharmProjects\StockNNv1\venv\lib\site-packages\tensorflow\python\keras\backend.py:4833 categorical_crossentropy
        target.shape.assert_is_compatible_with(output.shape)
    C:\Users\Technik\PycharmProjects\StockNNv1\venv\lib\site-packages\tensorflow\python\framework\tensor_shape.py:1134 assert_is_compatible_with
        raise ValueError("Shapes %s and %s are incompatible" % (self, other))

    ValueError: Shapes (35, 1) and (700, 35) are incompatible


Process finished with exit code 1

抱歉输出太长,但正如它显示的那样,train_dataset 有 700 个特征数字,而目标有 35 个数字,这就是我想要的。 (神经网络应该能够从 700 个给定值中预测 35 个值。)

1 个答案:

答案 0 :(得分:0)

我会做到以下几点:

import pandas as pd

model = tf.keras.Sequential([
    tf.keras.layers.Dense(700, activation='sigmoid', input_shape=(700,)),
    tf.keras.layers.Dense(1000, activation='sigmoid'),
    tf.keras.layers.Dense(500, activation='sigmoid'),
    tf.keras.layers.Dense(150, activation='sigmoid'),
    tf.keras.layers.Dense(35, activation='softmax')
])

model.summary()
model.compile(optimizer='adam',
              loss=tf.keras.losses.categorical_crossentropy,
              metrics=['accuracy'])
BATCH_SIZE = 8

train_x = pd.DataFrame(data=arrayInput)
train_y = pd.DataFrame(data=arrayTarget)

model.fit(x=train_x, y=train_y, epochs=EPOCHS, batch_size=BATCH_SIZE)

model.save('savedmodel')

我已将损失函数从 sparse_categorical_crossentropy 更改为 categorical_crossentropy,因为 sparse_categorical_crossentropy 期望目标为 int,但您的目标类型为 {{1} }.

如果您希望将 float 值作为目标,那么这不是分类问题而是 float 问题,因此您应该使用损失函数,例如 regression 或 { {1}} 并适当更改最后一层的激活函数。