【问题标题】:How can I feed my ndarray data to the TensorShape input of the basic Keras model?如何将我的 ndarray 数据提供给基本 Keras 模型的 TensorShape 输入?
【发布时间】:2019-04-16 17:58:05
【问题描述】:

我想将数据提供给基本的 keras 模型。输入具有以下形状和类型。不知道是不是我在模型层设置错了,但是出现如下错误。

我的环境是 Windows 10(64 位)、Python 3.6.7 (Anaconda)、TensorFlow 1.12.0、Keras 2.2.4、PyCharm 2018.3.3。

输入:

x 输入形状:(23714, 160),y 输入形状:(23714, 7)

In: x
Out:
array([[ 7, 19,  6, ...,  0,  0,  0],
       [11,  1, 16, ...,  0,  0,  0],
       [ 6, 13, 10, ...,  0,  0,  0],
       ...,
       [ 6, 13,  7, ...,  0,  0,  0],
       [11, 13,  9, ..., 10,  0,  0],
       [ 9, 13,  9, ...,  0,  0,  0]])
In: y
Out: 
array([[0, 1, 0, ..., 0, 0, 0],
       [0, 1, 0, ..., 0, 0, 0],
       [0, 1, 0, ..., 0, 0, 0],
       ...,
       [0, 0, 0, ..., 0, 0, 1],
       [0, 0, 0, ..., 0, 0, 1],
       [0, 0, 0, ..., 0, 0, 1]], dtype=int8)

型号:

model = Sequential()
model.add(Dense(64, input_dim=(160, ), activation='relu'))
model.add(Dense(7, activation="softmax"))
model.summary()
model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=["accuracy"])
performance_test = model.evaluate(x_test, y_test, batch_size=100)
print(model.summary())
print('Test Loss and Accuracy ->', performance_test)

错误:

Traceback (most recent call last):
  File "C:\Users\terry\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\eager\execute.py", line 141, in make_shape
    shape = tensor_shape.as_shape(v)
  File "C:\Users\terry\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\framework\tensor_shape.py", line 947, in as_shape
    return TensorShape(shape)
  File "C:\Users\terry\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\framework\tensor_shape.py", line 542, in __init__
    self._dims = [as_dimension(d) for d in dims_iter]
  File "C:\Users\terry\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\framework\tensor_shape.py", line 542, in <listcomp>
    self._dims = [as_dimension(d) for d in dims_iter]
  File "C:\Users\terry\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\framework\tensor_shape.py", line 482, in as_dimension
    return Dimension(value)
  File "C:\Users\terry\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\framework\tensor_shape.py", line 37, in __init__
    self._value = int(value)
TypeError: int() argument must be a string, a bytes-like object or a number, not 'tuple'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "C:\Users\terry\Anaconda3\envs\tensorflow\lib\site-packages\IPython\core\interactiveshell.py", line 3267, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-2-721312fb06f9>", line 1, in <module>
    runfile('C:/Users/terry/Desktop/Project/Test/Test.py', wdir='C:/Users/terry/Desktop/Project/Test')
  File "C:\Program Files\JetBrains\PyCharm 2018.3.3\helpers\pydev\_pydev_bundle\pydev_umd.py", line 197, in runfile
    pydev_imports.execfile(filename, global_vars, local_vars)  # execute the script
  File "C:\Program Files\JetBrains\PyCharm 2018.3.3\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "C:/Users/terry/Desktop/Project/Test/Test.py", line 187, in <module>
    model.add(Dense(64, input_dim=(160, ), activation='relu'))
  File "C:\Users\terry\Anaconda3\envs\tensorflow\lib\site-packages\keras\engine\sequential.py", line 161, in add
    name=layer.name + '_input')
  File "C:\Users\terry\Anaconda3\envs\tensorflow\lib\site-packages\keras\engine\input_layer.py", line 178, in Input
    input_tensor=tensor)
  File "C:\Users\terry\Anaconda3\envs\tensorflow\lib\site-packages\keras\legacy\interfaces.py", line 91, in wrapper
    return func(*args, **kwargs)
  File "C:\Users\terry\Anaconda3\envs\tensorflow\lib\site-packages\keras\engine\input_layer.py", line 87, in __init__
    name=self.name)
  File "C:\Users\terry\Anaconda3\envs\tensorflow\lib\site-packages\keras\backend\tensorflow_backend.py", line 517, in placeholder
    x = tf.placeholder(dtype, shape=shape, name=name)
  File "C:\Users\terry\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\ops\array_ops.py", line 1747, in placeholder
    return gen_array_ops.placeholder(dtype=dtype, shape=shape, name=name)
  File "C:\Users\terry\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\ops\gen_array_ops.py", line 6250, in placeholder
    shape = _execute.make_shape(shape, "shape")
  File "C:\Users\terry\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\eager\execute.py", line 143, in make_shape
    raise TypeError("Error converting %s to a TensorShape: %s." % (arg_name, e))
TypeError: Error converting shape to a TensorShape: int() argument must be a string, a bytes-like object or a number, not 'tuple'.

我读过有关 keras input_shape、input_dim、tensorflow tensorshape、python tuple、list、numpy ndarray 的信息,但我找不到解决方案。非常感谢您的帮助。

【问题讨论】:

    标签: python-3.x tensorflow input keras numpy-ndarray


    【解决方案1】:

    我认为Dense 的参数应该是input_shape 而不是input_dim。参考doc

    【讨论】:

    • 我真的很感谢你。我很担心这个问题并尝试了不同的方法,但只有将input_dim更改为input_shape才有效。
    • 为了清晰起见,另一个选项是使用 model.add(InputLayer(input_shape=(160,))) 开始 Sequential
    • @TheLoneDeranger 感谢您告诉我使用InputLayer。
    • 我还有一个问题。我知道如果我使用 RNN、LSTM 或 GRU,我需要做data = data.reshape (data.shape [0], 1, data.shape [1])。但是,我想知道当dense和conv1d一起使用reshape时是否有问题。
    • 是的,你可以reshape,然后发送到Dense和Conv1d层;那应该可以正常工作。根据数据在网络中的移动方式,您可能需要使用像 padding='same' 这样的 Conv1d 参数来维护数据形状等。一定要使用 model.summary() 来查看每一层如何改变形状,这会有所帮助带有连接层。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-19
    • 1970-01-01
    • 2018-05-16
    • 1970-01-01
    • 2018-01-15
    • 1970-01-01
    • 2020-11-15
    相关资源
    最近更新 更多