def train_lstm(): pred, _ = lstm(pm.batch_size) # 调用的构建的lstm变量 # 损失函数 平均平方误差(MSE) loss = tf.reduce_mean( tf.square(tf.reshape(pred, [-1]) - tf.reshape(Y, [-1]))) # 实现梯度下降算法的优化器,优化损失函数 train_op = tf.train.AdamOptimizer(pm.lr).minimize(loss) # 保存和恢复模型的方法;方法返回checkpoint文件的路径。可以直接传给restore() 进行调用 saver = tf.train.Saver(tf.global_variables()) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) # 重复训练10000次 for i in range(5): step = 0 start = 0 end = start + pm.batch_size while (end < len(train_x)): _, loss_ = sess.run([train_op, loss], feed_dict={ X: train_x[start:end], Y: train_y[start:end] }) start += pm.batch_size end = start + pm.batch_size # 每10步保存一次参数 if step % 10 == 0: print(i, step, loss_) print("保存模型:", saver.save(sess, save_path)) step += 1
word_to_index["the"], logfile=logpath, is_verbose=is_verbose) log("The word of index 20 is:", index_to_word[20], logfile=logpath, is_verbose=is_verbose) # lstm = LSTM(batch_size, embedding_size, vocab_size, hidden_size, max_size) x = tf.placeholder(tf.int32, (batch_size, max_size - 1), name="x") label = tf.placeholder(tf.int32, (batch_size, max_size - 1), name="label") teacher_forcing = tf.placeholder(tf.bool, (), name="teacher_forcing") word_embeddings, output, softmax_output = lstm(x, label, vocab_size, hidden_size, max_size, batch_size, embedding_size, teacher_forcing) with tf.Session() as sess: onehot = tf.argmax(softmax_output, 2) with tf.variable_scope("optimizer", reuse=tf.AUTO_REUSE): optimizer, loss, cross_entropy_out, weights = optimize( output, label, learning_rate) perplexity = tf.exp(cross_entropy_out) tf.summary.scalar('loss', loss) tf.summary.scalar('perplexity', perplexity) """Now let's execute the graph in the session. We ge a data batch with `dataloader.get_batch(batch_size)`. This fetches a batch of word sequences.
def __init__(self): self.name = "predictionApi" self.lstm = lstm() self.Company = Configuration().GetData()['CompanyList']
load_folder = False #'/home/osvald/Projects/Diagnostics/Sepsis/Models/lstm/32_2x64_l01_p25' offset = 0 n = 40336 n = 20000 split = 0.9 ind = list(range(n)) div = int(n * split) partition = dict([]) partition['train'] = list(ind[:div]) partition['validation'] = list(ind[div:n]) model = lstm(embedding=args.embedding, hidden_size=args.hidden, fcl=args.fcl, num_layers=args.layers, batch_size=batch_size, fcl_out=bool(args.fcl), embed=bool(args.embedding), droprate=args.drop).to(args.device) criterion = nn.BCEWithLogitsLoss(pos_weight=torch.DoubleTensor([1.8224]).to( args.device), reduction='none') optimizer = optim.SGD(model.parameters(), lr=args.lr, momentum=args.momentum) scheduler = optim.lr_scheduler.ReduceLROnPlateau(optimizer, verbose=False, factor=args.gamma, patience=args.patience) if load_model: model.load_state_dict(torch.load(load_model)) train_losses = np.concatenate(