为了进行评估,我需要能够对文本数据应用卷积层。因此,我试图对亚马逊评论进行情感分析。但是,在Embedding
层之后,Conv1D
层将无法获得所需的形状。
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
import tensorflow as tf
print(f'Tensorflow version {tf.__version__}')
from tensorflow import keras
from tensorflow.keras.layers import Dense, Conv1D, GlobalAveragePooling1D, Embedding
import tensorflow_datasets as tfds
from tensorflow.keras.models import Model
(train_data, test_data), info = tfds.load('imdb_reviews/subwords8k',
split=[tfds.Split.TRAIN, tfds.Split.TEST],
as_supervised=True, with_info=True)
padded_shapes = ([None], ())
train_dataset = train_data.shuffle(25000).padded_batch(padded_shapes=padded_shapes, batch_size=16)
test_dataset = test_data.shuffle(25000).padded_batch(padded_shapes=padded_shapes, batch_size=16)
n_words = info.features['text'].encoder.vocab_size
class ConvModel(Model):
def __init__(self):
super(ConvModel, self).__init__()
self.embe = Embedding(n_words, output_dim=16)
self.conv = Conv1D(32, kernel_size=6, activation='elu')
self.glob = GlobalAveragePooling1D()
self.dens = Dense(2)
def call(self, x, training=None, mask=None):
x = self.embe(x)
x = self.conv(x)
x = self.glob(x)
x = self.dens(x)
return x
conv = ConvModel()
conv(next(iter(train_data))[0])
ValueError:conv1d_25层的输入0与该层不兼容: 预期ndim = 3,找到的ndim = 2。收到完整的图形:[163,16]
如何实现这一目标,如果我错了,使用Conv1D
层为文本序列输入文本的正确方法是什么?
答案 0 :(得分:0)
它是conv(next(iter(train_dataset))[0])
而不是conv(next(iter(train_data))[0])
网络结构还可以
答案 1 :(得分:-1)
词嵌入层的out_dim应该与conv1D输入过滤器大小匹配。尝试将out_dim更改为32。正确的方法:https://machinelearningmastery.com/predict-sentiment-movie-reviews-using-deep-learning/