def get_data(index_dict,word_vectors,combined,y): n_symbols = len(index_dict) + 1 # 所有单词的索引数,频数小于10的词语索引为0,所以加1 embedding_weights = np.zeros((n_symbols, vocab_dim)) # 初始化 索引为0的词语,词向量全为0 for word, index in index_dict.items(): # 从索引为1的词语开始,对每个词语对应其词向量 embedding_weights[index, :] = word_vectors[word] x_train, x_test, y_train, y_test = train_test_split(combined, y, test_size=0.2) y_train = keras.utils.to_categorical(y_train,num_classes=3) y_test = keras.utils.to_categorical(y_test,num_classes=3) # print x_train.shape,y_train.shape return n_symbols,embedding_weights,x_train,y_train,x_test,y_test ##定义网络结构 def train_lstm(n_symbols,embedding_weights,x_train,y_train,x_test,y_test): print 'Defining a Simple Keras Model...' model = Sequential() # or Graph or whatever model.add(Embedding(output_dim=vocab_dim, input_dim=n_symbols, mask_zero=True, weights=[embedding_weights], input_length=input_length)) # Adding Input Length model.add(LSTM(output_dim=50, activation='tanh')) model.add(Dropout(0.5)) model.add(Dense(3, activation='softmax')) # Dense=>全连接层,输出维度=3 model.add(Activation('softmax')) print 'Compiling the Model...' model.compile(loss='categorical_crossentropy', optimizer='adam',metrics=['accuracy']) print "Train..." # batch_size=32 model.fit(x_train, y_train, batch_size=batch_size, epochs=n_epoch,verbose=1) print "Evaluate..." score = model.evaluate(x_test, y_test, batch_size=batch_size) yaml_string = model.to_yaml() with open('../model/lstm.yml', 'w') as outfile: outfile.write( yaml.dump(yaml_string, default_flow_style=True) ) model.save_weights('../model/lstm.h5') print 'Test score:', score
时间: 2024-04-26 12:26:40 浏览: 98
pytorch 状态字典:state_dict使用详解
5星 · 资源好评率100%
这段代码是用于训练一个简单的Keras模型,实现情感分析任务的。可以看出,该模型包括了嵌入层、LSTM层、Dropout层和全连接层。其中,嵌入层用于将单词转换为向量表示,LSTM层用于处理序列数据,Dropout层用于防止过拟合,全连接层用于输出分类结果。通过调整模型的参数,训练集和测试集的划分以及优化器等,可以得到不同的模型性能。
阅读全文