tensorflow 练习
来源:互联网 发布:网络教育发展趋势 编辑:程序博客网 时间:2024/06/02 22:00
import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_datamnist=input_data.read_data_sets("MNIST_data/",one_hot=True)sess=tf.InteractiveSession()x=tf.placeholder(tf.float32,[None,784])W=tf.Variable(tf.zeros([784,10]))b=tf.Variable(tf.zeros([10]))y=tf.nn.softmax(tf.matmul(x,W)+b)y_=tf.placeholder(tf.float32,[None,10])cross_entropy=tf.reduce_mean(-tf.reduce_sum(y_*tf.log(y),reduction_indices=[1]))train_step=tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)tf.global_variables_initializer().run()for i in range(1000): batch_xs,batch_ys=mnist.train.next_batch(100) train_step.run({x:batch_xs,y_:batch_ys})correct_prediction=tf.equal(tf.argmax(y,1),tf.argmax(y_,1))accuracy=tf.reduce_mean(tf.cast(correct_prediction,tf.float32))print(accuracy.eval({x:mnist.test.images,y_:mnist.test.labels}))
此处可能报找不到global_variables_initializer的错误,这个是由于你的tensorflow的版本过于低的问题导致的。
可以到https://github.com/tensorflow/tensorflow下载相应的.whl 用命令 sudo pip install XXXX.whl进行安装
import tensorflow as tffrom numpy.random import RandomStatebatch_size = 8x = tf.placeholder(tf.float32, shape=(None, 2), name="x-input")y_ = tf.placeholder(tf.float32, shape=(None, 1), name='y-input')w1= tf.Variable(tf.random_normal([2, 1], stddev=1, seed=1))y = tf.matmul(x, w1)# 定义损失函数使得预测少了的损失大,于是模型应该偏向多的方向预测。loss_less = 10loss_more = 1loss = tf.reduce_sum(tf.where(tf.greater(y, y_), (y - y_) * loss_more, (y_ - y) * loss_less))train_step = tf.train.AdamOptimizer(0.001).minimize(loss)rdm = RandomState(1)X = rdm.rand(128,2)Y = [[x1+x2+(rdm.rand()/10.0-0.05)] for (x1, x2) in X]with tf.Session() as sess: tf.global_variables_initializer().run() STEPS = 5000 for i in range(STEPS): start = (i*batch_size) % 128 end = (i*batch_size) % 128 + batch_size sess.run(train_step, feed_dict={x: X[start:end], y_: Y[start:end]}) if i % 1000 == 0: print("After %d training step(s), w1 is: " % (i)) print sess.run(w1), "\n" print "Final w1 is: \n", sess.run(w1)
简单的保存文件
import tensorflow as tfv1=tf.Variable(tf.constant(1.0,shape=[1]),name="v1")v2=tf.Variable(tf.constant(1.0,shape=[1]),name="v2")result=v1+v2init_op=tf.global_variables_initializer()saver=tf.train.Saver()with tf.Session() as sess: sess.run(init_op) saver.save(sess,"model.ckpt")
0 0
- tensorflow 练习
- TensorFlow练习23: “恶作剧”
- TensorFlow之神经网络练习
- Tensorflow教程_练习代码
- TensorFlow练习(一)——TensorFlow简介
- TensorFlow练习1: 对评论进行分类
- TensorFlow练习26: AI操盘手
- TensorFlow练习22: 手写汉字识别
- TensorFlow练习18: 根据姓名判断性别
- TensorFlow练习12: 判断男声女声
- TensorFlow练习7: 基于RNN生成古诗词
- TensorFlow练习2: 对评论进行分类
- Tensorflow-MNIST数字识别练习代码
- TensorFlow练习7: 基于RNN生成古诗词
- TensorFlow练习1: 对评论进行分类
- tensorflow练习1:利用神经网络进行分类
- TensorFlow入门简单机器学习练习
- tensorflow练习9:图像分类器
- 【Leetcode】45,55 Jump game
- Android优化篇——后台优化
- 当我们在谈大佬的努力的时候,到底在谈什么?
- bzoj 1634 贪心
- Java IO示例代码
- tensorflow 练习
- 为什么电脑WiFi图标会有一把红色的叉叉在上面?
- java-命令
- 与计算机的第一次对话“Hello world”
- Algorithm: Array(1)
- 转行穷三年?未必!
- bzoj 1656 bfs+射线法
- 友盟QQ第三方授权登陆(精简)
- lua变动参数累加_简单