首页 > 代码库 > 由浅入深之Tensorflow(2)----logic_regression实现
由浅入深之Tensorflow(2)----logic_regression实现
import tensorflow as tf import numpy as np from tensorflow.examples.tutorials.mnist import input_data def initWeights(shape): return tf.Variable(tf.random_normal(shape, stddev = 0.1)) def initBiases(shape): return tf.Variable(tf.random_normal(shape, stddev = 0.1)) def model(X, weights, baises): return tf.matmul(X, weights) + baises mnist = input_data.read_data_sets(‘MNIST_data/‘, one_hot = True) trX, trY, teX, teY = mnist.train.images, mnist.train.labels, mnist.test.images, mnist.test.labels X = tf.placeholder(‘float‘, [None, 784]) Y = tf.placeholder(‘float‘, [None, 10]) learning_rate = 0.05 epcoh = 100 weights = initWeights([784,10]) biases = initBiases([10]) y_ = model(X, weights, biases) cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(y_, Y)) train_op = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost) predict_op = tf.argmax(y_, 1) with tf.Session() as sess: tf.initialize_all_variables().run() for i in range(epcoh): for start, end in zip(range(0, len(trX), 128), range(128, len(trX)+1, 128)): sess.run(train_op, feed_dict = {X: trX[start:end], Y: trY[start:end]}) print (i, np.mean(np.argmax(teY, axis=1) == sess.run(predict_op, feed_dict={X: teX})))
由浅入深之Tensorflow(2)----logic_regression实现
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。