tensorflow 笔记
1. matMul
#-*-coding:utf-8 -*-
import tensorflow as tf w1 = tf.Variable(tf.random_normal([2,3], stddev= 1, seed= 1)) w2 = tf.Variable(tf.random_normal([3,1], stddev= 1, seed= 1)) x = tf.constant([[0.7, 0.9]]) # 1×2 a = tf.matmul(x, w1) # 1×3 y = tf.matmul(a, w2) # 1×1 sess = tf.Session() # w1 w2 sess.run(w1.initializer) sess.run(w2.initializer) sess.run(y) print(y) sess.close()
SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。
2. eval 函数 作用:
1.eval(): 将字符串string对象转化为有效的表达式参与求值运算返回计算结果
2.eval()也是启动计算的一种方式。基于Tensorflow的基本原理,首先需要定义图,然后计算图,其中计算图的函数常见的有run()函数,如sess.run()。同样eval()也是此类函数,
3.要注意的是,eval()只能用于tf.Tensor类对象,也就是有输出的Operation。对于没有输出的Operation, 可以用.run()或者Session.run();Session.run()没有这个限制。
import tensorflow as tf x = tf.Variable(3, name="x") y = tf.Variable(4, name="y") z = tf.Variable(4, name="z") w = tf.Variable(4, name="w") f = x * y * z + 3 - w //单个初始化,变量增多,还真是个事 with tf.Session() as sess: x.initializer.run() y.initializer.run() z.initializer.run() w.initializer.run() print(x) print(y) print(z) print(w) //f fuction eval 方式和js的eval一样,作为方法函数执行 result = f.eval() print(result)
3. 矩阵初始化 :
w1 = tf.constant([[1,1,1],[2,2,2]],tf.float32) w2 = tf.constant([[3],[3],[3]],tf.float32)
4. 使用GPU计算
g = tf.Graph() with g.device('/gpu:0'): a = tf.matmul(x, w1) # 1×3 y = tf.matmul(a, w2) # 1×1
5.变量
weights = tf.Variable(tf.random_normal([2,3], stddev = 2))// 产生2行3列 标准差为2 的变量
biases = tf.Variable(tf.zeros([3]) //初值为0 , 含有3个元素的变量
//使用w2变量初值来初始化w3
w2 = tf.Variable(weights.initialized_value())
w3 = tf.Varibale(weights.initialized_value() * 2.0)
6 tensorflow 训练神经网络
# 定义损失函数 cross_entropy = -tf.reduce_mean(y_ * tf/log(tf.clip_by_value(y, 1e-10, 1.0))) // -y_ln(y) # 定义学习率 learning_rate = 0.01 #定义反响传播算法 train_step = tf.train.AdamOptimizer(learning_rate).minimize(cross_entropy) //使损失函数最小
7. 完整的神经网络训练程序
# -*- coding: utf-8 -*- """ Created on Thu Apr 12 15:58:38 2018 @author: 无尾君 """ import tensorflow as tf from numpy.random import RandomState batch_size = 8 w1 = tf.Variable(tf.random_normal([2,3], stddev= 1, seed= 1)) w2 = tf.Variable(tf.random_normal([3,1], stddev= 1, seed= 1)) x = tf.placeholder(tf.float32, shape= (None,2), name= 'x-input') y_ = tf.placeholder(tf.float32, shape= (None,1), name= 'y-input') a = tf.matmul(x, w1) y = tf.matmul(a, w2) cross_entropy = -tf.reduce_mean(y_ * tf.log(tf.clip_by_value(y, 1e-10, 1.0))) train_step = tf.train.AdamOptimizer(0.001).minimize(cross_entropy) rdm = RandomState(1) dataset_size = 128 X = rdm.rand(dataset_size, 2) Y = [[int(x1+ x2 < 1)] for (x1, x2) in X] with tf.Session() as sess: tf.global_variables_initializer().run() print(sess.run(w1)) print(sess.run(w2)) STEPS = 5000 for i in range(STEPS): start = (i * batch_size) % dataset_size end = min(start + batch_size, dataset_size) sess.run(train_step, feed_dict = {x: X[start:end], y_: Y[start:end]}) if i % 1000 ==0: total_cross_entropy = sess.run(cross_entropy, feed_dict = {x: X, y_: Y}) print("After %d training step(s), cross entropy on all data is %g" % (i, total_cross_entropy))

更多精彩