训练模型卡在Google虚拟机计算引擎上

时间:2020-10-21 13:29:06

标签: tensorflow

您好,我正在参加Google tensorflow认证考试,讲师Laurence Moroney在NLP上载了一个ML模型,以预测Twitter消息的情绪。我可以在我的PC上训练模型,也可以在google colab中训练模型。

但是因为我的电脑是Surface Book 1 i5-6300u,并且没有GPU,而且深度学习模型需要大量的计算资源,所以我决定在Google云端上创建一个虚拟机,该虚拟机具有16vCPU和60 GB内存以及1个Tesla T4 GPU。我还需要使用Pycharm IDE进行考试。我正在使用Tensorflow 2.3.0和Python 3.8.6。

我完全可以在Google Cloud上训练其他模型。实际上,在Google云端上的培训比我自己的PC快8-10倍。

但是对于这个特定的模型,当我执行代码行时: history = model.fit(training_sequences, training_labels, epochs=num_epochs, validation_data=(test_sequences, test_labels), verbose=1)

培训陷入困境。但是,就像我之前说的那样,即使我的电脑运行缓慢并且在Google colab上进行培训也没有问题。我认为这是因为需要在嵌入层中使用预先训练的权重文件(Stanford的glove.6B.100d.txt)。

代码:

!wget --no-check-certificate \
    https://storage.googleapis.com/laurencemoroney-blog.appspot.com/glove.6B.100d.txt \
    -O /tmp/glove.6B.100d.txt
embeddings_index = {};
with open('/tmp/glove.6B.100d.txt') as f:
    for line in f:
        values = line.split();
        word = values[0];
        coefs = np.asarray(values[1:], dtype='float32');
        embeddings_index[word] = coefs;

embeddings_matrix = np.zeros((vocab_size+1, embedding_dim));
for word, i in word_index.items():
    embedding_vector = embeddings_index.get(word);
    if embedding_vector is not None:
        embeddings_matrix[i] = embedding_vector;

model = tf.keras.Sequential([
    tf.keras.layers.Embedding(vocab_size+1, embedding_dim, input_length=max_length, weights=[embeddings_matrix], trainable=False),
    tf.keras.layers.Dropout(0.2),
    tf.keras.layers.Conv1D(64, 5, activation='relu'),
    tf.keras.layers.MaxPooling1D(pool_size=4),
    tf.keras.layers.LSTM(64),
    tf.keras.layers.Dense(1, activation='sigmoid')
])
model.compile(loss='binary_crossentropy',optimizer='adam',metrics=['accuracy'])
model.summary()

num_epochs = 50

training_padded = np.array(training_sequences)
training_labels = np.array(training_labels)
testing_padded = np.array(test_sequences)
testing_labels = np.array(test_labels)

history = model.fit(training_padded, training_labels, epochs=num_epochs, validation_data=(testing_padded, testing_labels), verbose=2)

以下是Laurence Moroney的代码链接: link to NLP model on twitter sentiment

希望有人可以帮助我。我机智。并且只学习了2个月的机器学习,并且是金融硕士生的背景。任何能够回答这个问题的人都会非常感谢<3

0 个答案:

没有答案
相关问题