Google Colab和tensorflow:如何超调和保存模型

时间:2018-07-10 23:37:55

标签: python tensorflow neural-network classification google-colaboratory

我遵循了一些指南,并提出了这个神经网络:

with tf.device('/device:GPU:0'):
  with tf.Session(config=tf.ConfigProto(log_device_placement=True)) as sess:
      sess.run(tf.global_variables_initializer())

      for epoch in range(training_epochs):
          avg_cost = 0.0
          total_batch = int(len(X_train) / batch_size)
          x_batches = np.array_split(X_train, total_batch)
          y_batches = np.array_split(y_train, total_batch)
          for i in range(total_batch):
              batch_x, batch_y = x_batches[i], y_batches[i]
              _, c = sess.run([optimizer, cost], 
                              feed_dict={
                                  x: batch_x, 
                                  y: batch_y, 
                                  keep_prob: 0.8
                              })
              avg_cost += c / total_batch
          if epoch % display_step == 0:
              print("Epoch:", '%04d' % (epoch+1), "cost=", \
                  "{:.9f}".format(avg_cost))
      print("Optimization Finished!")
      correct_prediction = tf.equal(tf.argmax(predictions, 1), tf.argmax(y, 1))
      accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
      print("Accuracy:", accuracy.eval({x: X_test, y: y_test, keep_prob: 1.0}))

使用

cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=predictions, labels=y))
optimizer = tf.train.AdamOptimizer(learning_rate=0.0001).minimize(cost)

predictions = multilayer_perceptron(x, weights, biases, keep_prob)

n_hidden_1 = 300
n_input = X_train.shape[1]
n_classes = y_train.shape[1]

weights = {
    'h1': tf.Variable(tf.random_normal([n_input, n_hidden_1])),
    'out': tf.Variable(tf.random_normal([n_hidden_1, n_classes]))
}

biases = {
    'b1': tf.Variable(tf.random_normal([n_hidden_1])),
    'out': tf.Variable(tf.random_normal([n_classes]))
}

keep_prob = tf.placeholder("float")


training_epochs = 10000
display_step = 1000
batch_size = 32

x = tf.placeholder("float", [None, n_input])
y = tf.placeholder("float", [None, n_classes])

我的第一个问题:这是利用Google Colab提供的GPU的正确方法吗?我已经将硬件加速设置为gpu选项,但是我不确定事情的tensorflow方面。

我的第二个问题:如何保存模型?

我想让它运行一整夜,但是我可能无法在Google Colab提供的12小时内对其进行检查。如何保存模型以便以后返回模型?如何查看12小时后模型返回的精度?

我的第三个问题:是否有一种有效的方法(类似于sklearn中的randomsearchcv)来调整模型的参数?我曾考虑过创建类似于

的网格
param1 = []
param2 = []
etc...

for val1... in product(param1,param2)
    #train model
    # somehow return the best

但是我想知道是否有一种更可接受和更简化的方法来做到这一点,特别是对于随机搜索而不是网格

谢谢

编辑:

我相信这应该可以保存NN报告的准确性

  score = accuracy.eval({x: X_test, y: y_test, keep_prob: 1.0})
  new_df = pd.DataFrame({'Accuracy': [score]})
  out_path = 'Accuracy.xlsx'
  writer = pd.ExcelWriter(out_path, engine='openpyxl')
  new_df.to_excel(writer, sheet_name='Sheet 1')
  writer.save()

  files.download('Accuracy.xlsx')

0 个答案:

没有答案