python - 在向量上使用 cnn 而不是矩阵
问题描述
我想在向量而不是矩阵上使用卷积神经网络。我的输入数据是 70000 个字符串x_train
及其相应标签的列表y_train
。
这是 x_train 和 y_train 的前 5 个示例:
["Honestly, Buffalo is the correct answer. I remember people (somewhat) joking that Buffalo's mantra for starting goalies was "win a game, get traded".",
"I think Edmonton's front office was a travesty for the better part of 10 years, but Buffalo's systematic destruction of the term 'competitive' was much more responsible for the change to the draft lottery." ,
"Ah yes way could have been :( remember when he was drafted I thought he was gonna be great but nope could have had kawhi Thompson or jimmy butler
https://youtu.be/6xxbBR8iSZ0?t=40m49s If you didn't find it already.",
"Nothing out of the ordinary though, she just has eye constant eye contact.",
"He wouldn't have been a bad signing if we wouldn't have paid 18M euros. For the right price he would have been acceptable. "]
所以我将字符串更改为字符数组,如下所示:
x_train_array=[[]]*len(x_train)
for i in range(len(x_train)):
temp=list(x_train[i])
temp+=['0']*(400 - len(temp))
temp= [ord(c) for c in temp]
x_train_array[i]= np.expand_dims(temp, axis=0)
这样每个字符串现在都是一个 1 x 400 的向量。
我现在将数据集分为训练集和验证集:
epochs=20
batch_size=50
num_classes=20
x_train_split1=x_train_array[0:60000]
x_train_split2=x_train_array[60000:]
y_train_split1= y_train_neralnet[0:60000]
y_train_split2= y_train_neralnet[60000:]
现在我构建了一个卷积神经网络,如下所示:
model = Sequential()
model.add(Conv2D(64, kernel_size=(1, 9),
activation='relu',
input_shape=(1,400,1)))
model.add(Conv2D(128, (1, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(1, 2)))
model.add(Dropout(0.20))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.46))
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.Nadam(),
metrics=['accuracy'])
model.fit(x_train_split1, y_train_split1,
batch_size=batch_size,
epochs=epochs,
verbose=1,
validation_data=(x_train_split2, y_train_split2))
score = model.evaluate(x_train_split2, y_train_split2, verbose=0)
现在,当我尝试运行代码时,出现以下错误:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-3-63f2874efbe3> in <module>
65 epochs=epochs,
66 verbose=1,
---> 67 validation_data=(x_train_split2, y_train_split2))
68 score = model.evaluate(x_train_split2, y_train_split2, verbose=0)
69 print('Test loss:', score[0])
/opt/conda/lib/python3.6/site-packages/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_freq, max_queue_size, workers, use_multiprocessing, **kwargs)
1152 sample_weight=sample_weight,
1153 class_weight=class_weight,
-> 1154 batch_size=batch_size)
1155
1156 # Prepare validation data.
/opt/conda/lib/python3.6/site-packages/keras/engine/training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, check_array_lengths, batch_size)
577 feed_input_shapes,
578 check_batch_axis=False, # Don't enforce the batch size.
--> 579 exception_prefix='input')
580
581 if y is not None:
/opt/conda/lib/python3.6/site-packages/keras/engine/training_utils.py in standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
107 'Expected to see ' + str(len(names)) + ' array(s), '
108 'but instead got the following list of ' +
--> 109 str(len(data)) + ' arrays: ' + str(data)[:200] + '...')
110 elif len(names) > 1:
111 raise ValueError(
ValueError: Error when checking model input: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 1 array(s), but instead got the following list of 60000 arrays: [array([[ 72, 111, 110, 101, 115, 116, 108, 121, 44, 32, 66, 117, 102,
102, 97, 108, 111, 32, 105, 115, 32, 116, 104, 101, 32, 99,
111, 114, 114, 101, 99, 116, 32, 97, 110, ...
解决方案
首先,您应该对向量使用一维卷积。
推荐阅读
- c# - 天蓝色存储事件可以用于网络农场吗
- python - 无法在 MacOS Mojave 上的 Python 2.7 上安装 MySQLdb
- python - 如何为 StringIO 提供文件名并使用 Telegram Bot 发送?
- powerbi - Power Query 中是否有一种方法可以根据基于文本值重置的两列计算运行总计?
- javascript - iOS WKWebView 音频只播放一次
- reactjs - 带有错误的随机笑话测试失败无法读取未定义的属性
- spring - 无法将属性值注入类参数(@Value 注释)
- javascript - 仅使用一次按键触发功能?
- python - Panda DataFrame Plot xaxis.set_major_locator 在条形图上将刻度留空
- java - Java.security - 如何将 KeyPair 密钥存储在字符串中。我收到无效的 DER 编码异常