Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 我如何将我的ndarray数据输入到基本Keras模型的TensorShape输入?_Python 3.x_Tensorflow_Input_Keras_Numpy Ndarray - Fatal编程技术网

Python 3.x 我如何将我的ndarray数据输入到基本Keras模型的TensorShape输入?

Python 3.x 我如何将我的ndarray数据输入到基本Keras模型的TensorShape输入?,python-3.x,tensorflow,input,keras,numpy-ndarray,Python 3.x,Tensorflow,Input,Keras,Numpy Ndarray,我想把数据输入基本的keras模型。输入具有以下形状和类型。我不知道我是否在模型层中做了错误的设置,但是我得到了以下错误 我的环境是Windows 10 64位、Python 3.6.7 Anaconda、TensorFlow 1.12.0、Keras 2.2.4、PyCharm 2018.3.3 输入: x输入形状:23714160,y输入形状:23714,7 型号: 模型=顺序 model.addDense64,输入尺寸=160,激活='relu' model.addDense7,激活=so

我想把数据输入基本的keras模型。输入具有以下形状和类型。我不知道我是否在模型层中做了错误的设置,但是我得到了以下错误

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

输入:

x输入形状:23714160,y输入形状:23714,7

型号:

模型=顺序 model.addDense64,输入尺寸=160,激活='relu' model.addDense7,激活=softmax 范文摘要 model.compileloss=categorical\u crossentropy,optimizer=adam,metrics=[准确度] 性能测试=model.evaluatex测试,y测试,批量大小=100 printmodel.summary 打印“测试损失和准确性->”,性能测试 错误:

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元组、list、numpy ndarray的文章,但我找不到解决方案。非常感谢您的帮助。

我认为稠密的参数应该是input\u shape而不是input\u dim。参考

我真的很感谢你。我担心这个问题,并尝试了不同的方法,但只有当我将input_dim更改为input_shape时,它才会起作用。为了清晰起见,另一个选项是从模型开始顺序。addInputLayerinput_shape=160,@TheLonedRanger感谢您告诉我使用InputLayer。我还有一个问题。我知道如果我使用RNN、LSTM或GRU,我需要执行data=data.reformate data.shape[0],1,data.shape[1]。但是,我想知道当将“重塑”与“稠密”和“conv1d”一起使用时是否存在问题。是的,您可以重塑,然后发送到“稠密”和“conv1d”层;这应该行得通。根据数据在网络中的移动方式,您可能需要使用Conv1d参数,如padding='same'来维护数据形状等。确保使用model.summary查看每个层如何更改形状,这将有助于连接层。
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)
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'.