Python Keras Tensorflow在历元期间的整形误差

Python Keras Tensorflow在历元期间的整形误差,python,tensorflow,keras,reshape,Python,Tensorflow,Keras,Reshape,我是Keras和Tensorflow的新手,我正在尝试使用LSTM在我拥有的一些雷达脉冲数据集上进行训练。 在编译过程中,我让它工作,但当模型开始训练时,我无法让它工作。我不知道在哪里放置重塑以及使用哪些尺寸,因为错误给出的数字我不知道它们来自哪里 因此,代码是: x_train = load_data("D:\\Software_Herramienta\\Datasets\\sweep_switch_train.csv") y_train = load_data("D:\\Software_H

我是Keras和Tensorflow的新手,我正在尝试使用LSTM在我拥有的一些雷达脉冲数据集上进行训练。 在编译过程中,我让它工作,但当模型开始训练时,我无法让它工作。我不知道在哪里放置重塑以及使用哪些尺寸,因为错误给出的数字我不知道它们来自哪里

因此,代码是:

x_train = load_data("D:\\Software_Herramienta\\Datasets\\sweep_switch_train.csv")
y_train = load_data("D:\\Software_Herramienta\\Datasets\\sweep_switch_labels_train.csv")
x_train = x_train.reshape(-1, x_train.shape[0], x_train.shape[1], 1)
y_train.astype(int)
y_train = y_train.reshape(1,1000)

batch = 10

model = Sequential()
model.add(Convolution2D(32, 3, 3, activation='relu', input_shape=(None, x_train.shape[1], 1)))
model.add(TimeDistributed(Activation('relu')))
model.add(TimeDistributed(Dense(32, name="first_dense")))
model.add(Reshape((3, 4)))
model.add(LSTM(x_train.shape[1], dropout_U=0.2, dropout_W=0.2))
# model.add(Dense(1, activation='softmax'))

model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(x_train, y_train, epochs=15, batch_size=batch)
错误告诉我们:

>纪元1/15
2018-06-13 13:40:17.225066:it:\src\github\tensorflow\tensorflow\core\platform\cpu\u feature\u guard.cc:140]您的cpu支持此tensorflow二进制文件未编译为使用的指令:AVX2
回溯(最近一次呼叫最后一次):
文件“D:\Software\u Utils\Python\u 3.6.5\lib\site packages\tensorflow\Python\client\session.py”,第1322行,在\u do\u调用中
返回fn(*args)
文件“D:\Software\u Utils\Python\u 3.6.5\lib\site packages\tensorflow\Python\client\session.py”,第1307行,在\u run\u fn中
选项、提要、获取列表、目标列表、运行元数据)
文件“D:\Software\u Utils\Python\u 3.6.5\lib\site packages\tensorflow\Python\client\session.py”,第1409行,位于调用会话运行中
运行(元数据)
tensorflow.python.framework.errors\u impl.InvalidArgumentError:重塑的输入是一个值为31872128的张量,但请求的形状有12个
[[Node:Reformate_1/Reformate=Reformate[T=DT_FLOAT,Tshape=DT_INT32,_device=“/job:localhost/replica:0/task:0/device:CPU:0”](time\u distributed_2/add,Reformate_1/Reformate/shape)]]
在处理上述异常期间,发生了另一个异常:
回溯(最近一次呼叫最后一次):
文件“D:/Software\u Herramienta/Pulse\u Generator/Pulse\u model\u keras\u tf.py”,第74行,in
模型拟合(x_序列,y_序列,历代=15,批量大小=批量)
文件“D:\Software\u Utils\Python\u 3.6.5\lib\site packages\keras\models.py”,第1002行,以适当方式
验证步骤=验证步骤)
文件“D:\Software\u Utils\Python\u 3.6.5\lib\site packages\keras\engine\training.py”,第1705行,以适合的形式
验证步骤=验证步骤)
文件“D:\Software\u Utils\Python\u 3.6.5\lib\site packages\keras\engine\training.py”,第1236行,在_fit\u循环中
outs=f(ins\U批量)
文件“D:\Software\u Utils\Python\u 3.6.5\lib\site packages\keras\backend\tensorflow\u backend.py”,第2482行,在调用中__
**自我介绍(kwargs)
文件“D:\Software\u Utils\Python\u 3.6.5\lib\site packages\tensorflow\Python\client\session.py”,第900行,正在运行
运行_元数据_ptr)
文件“D:\Software\u Utils\Python\u 3.6.5\lib\site packages\tensorflow\Python\client\session.py”,第1135行,处于运行状态
feed_dict_tensor、options、run_元数据)
文件“D:\Software\u Utils\Python\u 3.6.5\lib\site packages\tensorflow\Python\client\session.py”,第1316行,在运行中
运行(元数据)
文件“D:\Software\u Utils\Python\u 3.6.5\lib\site packages\tensorflow\Python\client\session.py”,第1335行,在\u do\u调用中
提升类型(e)(节点定义、操作、消息)
tensorflow.python.framework.errors\u impl.InvalidArgumentError:重塑的输入是一个值为31872128的张量,但请求的形状有12个
[[Node:Reformate_1/Reformate=Reformate[T=DT_FLOAT,Tshape=DT_INT32,_device=“/job:localhost/replica:0/task:0/device:CPU:0”](time\u distributed_2/add,Reformate_1/Reformate/shape)]]
由op“重塑1/重塑”引起,定义为:
文件“D:/Software\u Herramienta/Pulse\u Generator/Pulse\u model\u keras\u tf.py”,第69行,in
添加模型(重塑((3,4)))
文件“D:\Software\u Utils\Python\u 3.6.5\lib\site packages\keras\models.py”,第522行,在add中
输出张量=层(自输出[0])
文件“D:\Software\u Utils\Python\u 3.6.5\lib\site packages\keras\engine\topology.py”,第619行,在调用中__
输出=自调用(输入,**kwargs)
文件“D:\Software\u Utils\Python\u 3.6.5\lib\site packages\keras\layers\core.py”,第406行,在调用中
返回K.重塑(输入,(K.形状(输入)[0],)+self.目标形状)
文件“D:\Software\u Utils\Python\u 3.6.5\lib\site packages\keras\backend\tensorflow\u backend.py”,第1898行,位于重塑中
返回tf。重塑(x,形状)
文件“D:\Software\u Utils\Python\u 3.6.5\lib\site packages\tensorflow\Python\ops\gen\u array\u ops.py”,第7323行,在整形中
“重塑”,张量=张量,形状=形状,名称=名称)
文件“D:\Software\u Utils\Python\u 3.6.5\lib\site packages\tensorflow\Python\framework\op\u def\u library.py”,第787行,位于“应用”op\u helper中
op_def=op_def)
文件“D:\Software\u Utils\Python\u 3.6.5\lib\site packages\tensorflow\Python\framework\ops.py”,第3392行,位于create\u op
op_def=op_def)
文件“D:\Software\u Utils\Python\u 3.6.5\lib\site packages\tensorflow\Python\framework\ops.py”,第1718行,在uu init中__
self._traceback=self._graph._extract_stack()35; pylint:disable=protected access
InvalidArgumentError(回溯见上文):重塑的输入是一个值为31872128的张量,但请求的形状有12个
[[Node:Reformate_1/Reformate=Reformate[T=DT_FLOAT,Tshape=DT_INT32,_device=“/job:localhost/replica:0/task:0/device:CPU:0”](time\u distributed_2/add,Reformate_1/Reformate/shape)]]

作为
重塑层输入的元素数量必须等于其输出中的元素数量(
输出\u形状=(无,3,4)
,因此
批量大小*12
元素),目前情况并非如此

您的模型仍在编译,因为您正在为模型输入的维度
0
引入动态大小(c.f.
input\u shape=(无,x\u train.shape[1],1)
)。因为
None
将仅在推断时被实际维度大小替换,所以Keras在编译时信任您为样本提供适当大小的维度
0
,以便它们在
重塑之前的元素数量等于
3*4=12
。由于最后使用
model.fit()
馈送的元素最终会变大,因此在
重塑
时推理失败

具体地说,让我们分析一下您的代码。根据跟踪中的值,我将假设您的
扫描开关\u train.cs
# Mocking 50 samples of size 1000x1000x1 and their labels:
x_train = np.random.rand(50, 1000, 1000, 1)
y_train = np.random.randint(2, size=50)
model = Sequential()
model.add(Convolution2D(32, 3, 3, activation='relu', input_shape=x_train.shape[1:]))
# notice we set input_shape to (1000, 1000, 1) instead of (None, 1000, 1) the way you were.
# This makes the example clearer as Keras directly knows all dimensions.
model.add(TimeDistributed(Activation('relu')))
model.add(TimeDistributed(Dense(32, name="first_dense")))
model.add(Reshape((3, 4)))
model.add(LSTM(x_train.shape[1], dropout_U=0.2, dropout_W=0.2))
model = Sequential()
model.add(Convolution2D(32, 3, 3, activation='relu', input_shape=x_train.shape[1:]))
print(model.layers[0].output_shape)
# (None, 998, 998, 32)
model.add(TimeDistributed(Activation('relu')))
print(model.layers[1].output_shape)
# (None, 998, 998, 32)
model.add(TimeDistributed(Dense(32, name="first_dense")))
print(model.layers[2].output_shape)
# (None, 998, 998, 32)
model.add(Reshape((3, 4)))