本文介绍了在CPU上使用卷积层时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在没有gpu支持的情况下使用keras的卷积层吗?当我在Colab上使用运行时为None时出现错误.我的代码如下:

 模型= tf.keras.Sequential()model.add(layers.Conv1D(1,5,name ='conv1',padding ="same",activation ='relu',data_format ="channels_first",input_shape =(1,2048)))#model.add(layers.LSTM(5,activation ='tanh'))model.add(layers.Flatten())model.add(layers.Dense(512,activation ='relu'))model.add(layers.Dense(num_classes,activation ='softmax'))#model.summary()model.compile(loss = tf.keras.losses.categorical_crossentropy,Optimizer = tf.keras.optimizers.SGD(lr = 0.001,动量= 0.9),指标= ['准确性'])x_train =火车值y_train =火车标签x_test =测试值y_test = test_labelprint(np.shape(x_train))#x火车的形状是(4459,1,2048)print(np.shape(x_test))#测试的形状为(1340,1,2048)历史= model.fit(x_train,y_train,batch_size = 100,epochs = 30,verbose = 1,validation_data =(x_test,y_test)) 

它在GPU上运行良好,但在CPU上出现以下错误:

我发现问题出在输入数据的格式上.我的输入数据是大小为(1,2048)的向量.您能指导我如何将这些向量转换为NHWC格式吗?如果有人可以帮我解决这个问题,我将不胜感激.提前致谢.

解决方案

Keras文档

TensorFlow中的现在Keras似乎根据 Conv2D 运算符实现了 Conv1D -基本上形成了具有1行的 W 的图像"列,然后是您的 C 渠道".这就是为什么当您没有图像数据时会收到有关图像形状的错误消息的原因.

在上面的文档中,通道"是每个时间步的数据项数(例如,也许每个时间步有5个传感器读数,所以您有5个通道).从上面的答案中,我相信您正在传递形状为(n,1,2048)的张量,其中 n 是您的批处理大小.因此,使用 channels_last 时,TensorFlow认为这意味着您的批次中有 n 个示例,每个示例的序列长度分别为 1 2048 每个时间步的数据项-只是单个时间步,每个观测值有2048个数据项(例如,每个时间步获取2048个传感器读数),在这种情况下,卷积将不会进行卷积-等效到单个密集层,将所有2048个数字作为输入.

我认为实际上您每个时间步长只有一个数据项,而您却有2048个时间步长.这就解释了为什么通过 channels_first 可以提高您的准确性-现在TensorFlow理解您的数据代表1个数据项样本2048次,并且可以对该数据进行卷积.

要解决此问题,您只需 tf.reshape(t,(1,2048,1))-并删除 channels_first (该代码假定您正在批量处理)大小为1,并且您的张量名为 t ).现在,其格式为(n,s,1),其中 n 是批处理大小(此处为 1 ), s 是时间步长(2048), 1 表示每个时间步长一个数据点.现在,您可以在GPU或CPU上运行相同的模型.

Can I use Convolutional layers of keras without gpu support? I am getting errors when I use it on Colab with runtime as None.My code looks like this:

model = tf.keras.Sequential()
model.add(layers.Conv1D(1,5, name='conv1', padding="same", activation='relu',data_format="channels_first", input_shape=(1,2048)))
# model.add(layers.LSTM(5, activation='tanh'))
model.add(layers.Flatten())
model.add(layers.Dense(512, activation='relu'))
model.add(layers.Dense(num_classes, activation='softmax'))
#model.summary()
model.compile(loss=tf.keras.losses.categorical_crossentropy,
              optimizer=tf.keras.optimizers.SGD(lr=0.001, momentum=0.9),
              metrics=['accuracy'])

x_train = train_value
y_train = train_label
x_test = test_value
y_test = test_label
print(np.shape(x_train)) #shape of x train is (4459, 1, 2048)
print(np.shape(x_test)) #shape of test is (1340,1,2048)

history = model.fit(x_train, y_train,
          batch_size=100,
          epochs=30,
          verbose=1,
          validation_data=(x_test, y_test)

          )

It is running fine on GPU but gives following error on CPU:

I have figured out that the problem is with the format of Input Data. My input data are vectors of size (1,2048). Can you please guide me on how to convert these vectors to NHWC format?I would really appreciate it, if someone can clear this up for me.Thanks in advance.

解决方案

Per the Keras documentation

Now Keras in TensorFlow appears to implement Conv1D in terms of a Conv2D operator - basically forming an "image" with 1 row, W columns, and then your C "channels". That's why your getting error messages about image shapes when you don't have image data.

In the docs above "channels" are the number of data items per time step (e.g. perhaps you have 5 sensor readings at each time step so you'd have 5 channels). From your answers above I believe you're passing tensors with shape (n, 1, 2048) where n is your batch size. So, with channels_last TensorFlow thinks that means you have n examples in your batch each with a sequence length of 1 and 2048 data items per time step - that is only a single time step with 2048 data items per observation (e.g. 2048 sensor readings taken at each time step) in which case the convolution won't be doing a convolution - it'd be equivalent to a single dense layer taking all 2048 numbers as input.

I think in reality you have only a single data item per time step and you have 2048 time steps. That explains why passing channels_first improves your accuracy - now TensorFlow understand that your data represents 1 data item samples 2048 times and it can do a convolution over that data.

To fix you can just tf.reshape(t, (1, 2048, 1)) - and remove the channels_first (that code assumes you're doing batches of size 1 and your tensor is named t). Now it's in the format (n, s, 1) where n is the batch size (1 here), s is the number of time steps (2048), and 1 indicates one data point per time step. You can now run the same model on the GPU or CPU.

这篇关于在CPU上使用卷积层时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-13 10:11