【发布时间】:2018-09-27 18:28:56
【问题描述】:
在训练过程中,我使用 train_test_split() 将数据集拆分为训练和测试数据:
from sklearn.model_selection import train_test_split
import tensorflow as tf
import numpy as np
X_train, X_test, y_train, y_test = train_test_split(data, labels, test_size=0.33, random_state=28)
with tf.Session(graph=graph) as sess:
sess.run(tf.global_variables_initializer())
print ('History length: ',history)
saver = tf.train.Saver()
writer = tf.summary.FileWriter("logs", sess.graph)
plt.ion()
plt.show()
for epoch in range(num_epochs):
shuffle_ind=np.random.permutation(X_train.shape[0])
y_train=y_train.iloc[shuffle_ind,:]
X_train=X_train[shuffle_ind,:]
for batch_no in range(X_train.shape[0]//batch_size):
seq_len=[history]*batch_size
batch_X=X_train[batch_no*batch_size:(batch_no+1)*batch_size,:]
batch_y=y_train.iloc[batch_no*batch_size:(batch_no+1)*batch_size,:]
feed = {data_pl: batch_X, target_pl: batch_y.iloc[:,1:], seq_len_pl:seq_len,keep_prob_pl:0.5} #1.0
_,batch_loss = sess.run([train_op,tf_loss], feed_dict=feed)
feed = {data_pl: X_test, target_pl: y_test.iloc[:,1:], seq_len_pl:[history]*X_test.shape[0],keep_prob_pl:0.5}
test_loss,predictions,acc_np = sess.run([tf_loss,preds,tf_acc], feed_dict=feed)
p_=np.argmax(predictions, axis=1)
l_=np.argmax(np.array(y_test.iloc[:,1:]),axis=1)
acc = sum(p_==l_)/float(len(p_))
print ("train_acc: ", acc, "test_acc: ", acc_np)
feed = {data_pl: X_train, target_pl: y_train.iloc[:,1:], seq_len_pl:[history]*X_train.shape[0],keep_prob_pl:0.5}
train_loss = sess.run(tf_loss, feed_dict=feed)
print ("Train loss: ",train_loss," Test loss: ",test_loss)
在训练并获得准确率之后,如何将这个训练好的模型应用于整个数据集,而不仅仅是测试数据?
【问题讨论】:
标签: python tensorflow neural-network