谁能为我推荐解决此类错误的最佳途径?我无法弄清楚尺寸方面做错了什么。我有一个源于Word2Vec gensim模型的预训练嵌入,我想使用该模型来初始化CNN。抱歉,这个问题相对简单,但对于Keras和Tensorflow来说都是新事物
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>ch.uzh.ifi.csg.cloudsim</groupId>
<artifactId>rda</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<name>Cloudsim RDA (Resource Dependency Aware)</name>
<url>https://github.com/pattad/cloudsim-rda</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.cloudbus.cloudsim</groupId>
<artifactId>cloudsim</artifactId>
<version>3.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-math3</artifactId>
<version>3.3</version>
</dependency>
</dependencies>
<description>This module enhances the cloud simulator project cloudsim (see http://www.cloudbus.org/cloudsim) with resource dependency aware workloads.</description>
</project>
输出为:
#CNN architecture
num_classes = num_labels
#Training params
batch_size = 8
num_epochs = 25
#Model parameters
num_filters = 64
weight_decay = 1e-4
kernel_size = 7 #this is the size of the window during convolution...making match the window size in Word2Vec...unsure if needed
print("training CNN ...")
model = Sequential()
#------------------------
FIXED_LENGTH=embedding_matrix.shape[1]
#------------------------
print('Vocab size:', vocab_size)
print('Output_Dim size:', w2v.vector_size)
print('Weights:', pd.Series([embedding_matrix]).shape)
print('Weights underlying shape:', embedding_matrix.shape)
print("Input Length:", FIXED_LENGTH)
#Model add word2vec embedding
model.add(Embedding(vocab_size+1,
output_dim=w2v.vector_size,
weights=[embedding_matrix],
input_length=FIXED_LENGTH,
trainable=False))
model.add(Conv1D(num_filters, kernel_size=kernel_size, activation='relu', padding='same'))
model.add(MaxPooling1D(2))
model.add(Conv1D(num_filters, 7, activation='relu', padding='same'))
model.add(GlobalMaxPooling1D())
model.add(Dropout(0.5))
model.add(Dense(32, activation='relu', kernel_regularizer=regularizers.l2(weight_decay)))
model.add(Dense(num_classes, activation='softmax')) #multi-label (k-hot encoding)
adam = Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0)
model.compile(loss='sparse_categorical_crossentropy', optimizer=adam, metrics=['accuracy'])
model.summary()
#define callbacks
early_stopping = EarlyStopping(monitor='val_loss', min_delta=0.01, patience=4, verbose=1)
callbacks_list = [early_stopping]
print('Batch size:', batch_size)
print('Num of Epochs:', num_epochs)
print('X Train Size:', x_train_pad.shape)
print('Y Train Size:', y_train.shape)
hist = model.fit(x_train_pad,
y_train,
batch_size=batch_size,
epochs=num_epochs,
callbacks=callbacks_list,
validation_split=0.1,
shuffle=True,
verbose=2)
答案 0 :(得分:0)
可以将嵌入层中的vocab_size+1
参数更改为vocab_size
。我认为是造成问题的+1
答案 1 :(得分:0)
答案是编码的句子包含的值比词典构建阶段中编码的值高。在您的词典中,应该为您的培训和测试集的每个值提供一个索引。如果没有,则必须先清理句子,然后再将其发送到CNN。