Task2 SGD
来源:互联网 发布:网络诊断dns未响应原因 编辑:程序博客网 时间:2024/06/05 18:51
这次的作业是把logistics regression改成一个简单的神经网络
激活函数是一个ReLU函数,实现非线性化,之后输出的值传递给普通的logistics regression的输入即可
batch_size = 128graph = tf.Graph()with graph.as_default(): # Input data. For the training data, we use a placeholder that will be fed # at run time with a training minibatch. tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) tf_valid_dataset = tf.constant(valid_dataset) tf_test_dataset = tf.constant(test_dataset) # Variables. weights1 = tf.Variable( tf.truncated_normal([image_size * image_size, 1024])) biases1 = tf.Variable(tf.zeros([1024])) weights2 = tf.Variable( tf.truncated_normal([1024, num_labels])) biases2 = tf.Variable(tf.zeros([num_labels])) # Training computation. #input to hidden layer z1 = tf.matmul(tf_train_dataset, weights1) + biases1 h1 = tf.nn.relu(z1) #hidden layer to output logits = tf.matmul(h1, weights2) + biases2 loss = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(labels=tf_train_labels, logits=logits)) # Optimizer. optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) # Predictions for the training, validation, and test data. train_prediction = tf.nn.softmax(logits) valid_z1 = tf.matmul(tf_valid_dataset, weights1) + biases1 valid_h1 = tf.nn.relu(valid_z1) #hidden layer to output valid_logits = tf.matmul(valid_h1, weights2) + biases2 valid_prediction = tf.nn.softmax(valid_logits) test_z1 = tf.matmul(tf_test_dataset, weights1) + biases1 test_h1 = tf.nn.relu(test_z1) #hidden layer to output test_logits = tf.matmul(test_h1, weights2) + biases2 test_prediction = tf.nn.softmax(test_logits)
训练
num_steps = 3001with tf.Session(graph=graph) as session: tf.global_variables_initializer().run() print("Initialized") for step in range(num_steps): # Pick an offset within the training data, which has been randomized. # Note: we could use better randomization across epochs. offset = (step * batch_size) % (train_labels.shape[0] - batch_size) # Generate a minibatch. batch_data = train_dataset[offset:(offset + batch_size), :] batch_labels = train_labels[offset:(offset + batch_size), :] # Prepare a dictionary telling the session where to feed the minibatch. # The key of the dictionary is the placeholder node of the graph to be fed, # and the value is the numpy array to feed to it. feed_dict = {tf_train_dataset : batch_data, tf_train_labels : batch_labels} _, l, predictions = session.run( [optimizer, loss, train_prediction], feed_dict=feed_dict) if (step % 500 == 0): print("Minibatch loss at step %d: %f" % (step, l)) print("Minibatch accuracy: %.1f%%" % accuracy(predictions, batch_labels)) print("Validation accuracy: %.1f%%" % accuracy( valid_prediction.eval(), valid_labels)) print("Test accuracy: %.1f%%" % accuracy(test_prediction.eval(), test_labels))
结果
0 0
- Task2 SGD
- task2 treeview
- task2 treeview2
- task2 treeview3
- Task2相关总结
- linux-0.11调试教程,task2
- DCASE2017 task2 gpu报错
- 4PM Task2 successful version(20110318)
- 百度前端学院2015task2自学总结
- 百度前端学院2016task2自学笔记
- 【雅思备考】Task2——总纲领
- 【雅思备考】Task2——主体段
- 【雅思备考】Task2——总阶段
- [SGD]--Optimization(SGD, Momentum, Adagrad, Adadelta)
- SGD-SVM Spark并行
- SGD(随机梯度下降)
- Caffe SGD shuffle mechanism
- 异步的SGD
- 变量的声明与定义以及关键字extern的用法
- shell脚本后数据写入变量
- iframe 标签
- 总结一下今天的几点收获
- Js快速获取图片的大小
- Task2 SGD
- iframe的常用方法
- 调试
- Android自定义类似ProgressDialog效果的Dialog
- 初识vue.js
- LINUX 文件系统根目录 挂载点间的区别与联系
- Mybatis 获得自动生成主键值
- phpmailer--中文说明
- LitePal 使用方法