速通过tensorflow1.X

tensorflow 1.X

tensoeflow虽然已经发布了2.0,但是1.0的代码结构却和2的差距很大,所以一天学一下结构。

1.基本概念

1.1 构建结构图

  • 图(graphs)来表示计算任务

  • Session(绘画)来执行图

  • tensor来表示数据

  • Variable来维护状态

图用于表示计算任务,其中由节点构成(operation),而每个节点可以由很多个Tensor,图需要在session中才可以被使用。

%tensorflow_version 1.x # colab默认版本为tensorflow2 这里需要设置才可以用tf1
import tensorflow as tf

m1 = tf.constant([[3,3]])
m2 = tf.constant([[2],[3]])

product = tf.matmul(m1,m2)
print('product=',product)

# create session
sess = tf.Session()

result = sess.run(product)
print('result=',result

产生的结果如下

product= Tensor("MatMul_5:0", shape=(1, 1), dtype=int32)
result= [[15]]

product是个张量的类型,而result才是一个一维向量

在这里插入图片描述

1.2 变量

​ tensorflow1.X可以设计变量,但是使用之前必须是要初始化的tf.global_variable_initializer()才可以使用。这里由两个例子,第一个例子是变量的使用,这里完成了简单的加减法。同时也展示了sess的用法

x = tf.Variable([1,2])
a = tf.constant([3,3])

# 如果存在变量必须初始化操作
init  = tf.global_variables_initializer()

sub = tf.subtract(x,a)
add = tf.add(sub,a)


with tf.Session() as sesss:
  sesss.run(init)# 变量的初始化1
  print(sesss.run(sub))
  print(sesss.run(add)

第二个完成了一个循环叠加的例子,tensorflow中的赋值,用到的是tf.assign

# create var
state = tf.Variable(0,name='pikachu')

new_value = tf.add(state,1)
update = tf.assign(state,new_value)

init  = tf.global_variables_initializer()

with tf.Session() as sess:
  sess.run(init)
  print(sess.run(state))

  for _ in range(5):
    sess.run(update)
    print(sess.run(state)

结果显示如下

在这里插入图片描述

1.3 fetch 和feed

fetch完成的工作就是同时运行多个op,多节点同时进行的工作。将多个运算等式同时输入到sess当中即可实现fetch的效果。

feed可以后面再传入数值,先定义占位的空字符,后续在sess中传入数值。而常量constant是不可以后续进行赋值的。feed的数据以字典的形式传入

A1 = tf.placeholder(tf.float32)

A2 = tf.placeholder(tf.float32)
Re = tf.multiply(A1,A2)
Re2 = tf.add(A1,A2)
with tf.Session() as sess:
  print(sess.run(Re,feed_dict={A1:[7.],A2:[2.]}))

实例

#示例

import numpy as np

x_data = np.random.rand(100)
y_data = x_data*0.1 + 0.2

## 搭建网络
b = tf.Variable(0.)
w = tf.Variable(0.)
y = w * x_data + b

# 代价函数
loss = tf.reduce_mean(tf.square(y_data - y))
optimizer = tf.train.GradientDescentOptimizer(0.1)

train = optimizer.minimize(loss)

init = tf.global_variables_initializer()

with tf.Session() as sess:
  sess.run(init)
  for step in range (501):
    sess.run(train)
    if step%50 ==0:
      print('step=',step,'w,b = ',sess.run([w,b]))

训练结果如下

step= 0 w,b =  [0.02515155, 0.049536534]
step= 50 w,b =  [0.10068756, 0.19965051]
step= 100 w,b =  [0.10036986, 0.19981205]
step= 150 w,b =  [0.100198954, 0.19989891]
step= 200 w,b =  [0.10010699, 0.19994564]
step= 250 w,b =  [0.100057565, 0.19997075]
step= 300 w,b =  [0.10003097, 0.19998425]
step= 350 w,b =  [0.10001668, 0.19999151]
step= 400 w,b =  [0.10000902, 0.19999541]
step= 450 w,b =  [0.10000489, 0.1999975]
step= 500 w,b =  [0.10000266, 0.19999863]

2 搭建一些网络

BP神经网络

import matplotlib.pyplot as plt

x_data = np.linspace(-1,1,500)[:,np.newaxis]
noise = np.random.normal(0,0.02,x_data.shape)
y_data = np.square(x_data) + noise

# print(noise)

x = tf.placeholder(tf.float32,[None,1])
y = tf.placeholder(tf.float32,[None,1])

# BPnet
W1 = tf.Variable(tf.random_normal([1,10]))
B1 = tf.Variable(tf.zeros([1,10]))
w_b_1 = tf.matmul(x,W1) +B1
L1 = tf.nn.tanh(w_b_1)

## output


W2 = tf.Variable(tf.random_normal([10,1]))
B2 = tf.Variable(tf.zeros([1,1]))
w_b_2 = tf.matmul(L1,W2) +B2
#prediction = tf.nn.tanh(w_b_2)

loss = tf.reduce_mean(tf.square(y - w_b_2))
train = tf.train.GradientDescentOptimizer(0.1).minimize(loss)

init = tf.global_variables_initializer()

with tf.Session() as sess:
  sess.run(init)
  for step in range(2000):
    sess.run(train,feed_dict = {x:x_data,y:y_data})

  prediction_value = sess.run(w_b_2,feed_dict={x:x_data})
  plt.figure()
  plt.scatter(x_data,y_data)
  plt.plot(x_data,prediction_value,'r-',lw=5)
  plt.show()

搭建网络是简单的,所以这里尝试使用不同激活函数查看产生的结果

softsign()

在这里插入图片描述

tanh():

在这里插入图片描述

tensorboard

定义一个命名空间,把变量放到一起,然后在后面的session中,调用函数,生成logs文件夹,然后使用命令

with tf.name_scope('pokemon'):

  x = tf.placeholder(tf.float32,[None,1],name = 'x')
  y = tf.placeholder(tf.float32,[None,1],name = 'y')
  
  
  ...
  
with tf.Session() as sess:
  sess.run(init)
  writer =tf.summary.FileWriter('./logs',sess.graph)

终端输入$ tensorboard --logdir='PWD'

搭建一个识别mnist的网络

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data

mnist = input_data.read_data_sets('MNIST_data',one_hot=True)

batch_size = 100
n = mnist.train.num_examples // batch_size
print('n=',n)

def weight(shape):
  initial = tf.truncated_normal(shape,stddev = 0.1)
  return tf.Variable(initial)

def bias(shapee):
  initial = tf.constant(0.1,shape = shapee)
  return tf.Variable(initial)


def conv_layer(x,W):
  # x:输入的张量,四维的值(batch,高,宽,通道数)
  # W:卷积核(h,w,in_ch,ou_ch)
  # 卷积的步长。[1],[2]分别表示横纵的步长
  return tf.nn.conv2d(x,W,strides=[1,1,1,1],padding = 'SAME')


def max_pool_2x2(x):
  # ksize 设置池化的窗口大小(中间两位2*2)
  return tf.nn.max_pool(x,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')

x = tf.placeholder(tf.float32,[None,784])
y = tf.placeholder(tf.float32,[None,10])

x_img = tf.reshape(x,[-1,28,28,1])# 后续会自动更新为100

# VGG13
#layer1
W1 = weight([5,5,1,64])
B1 = bias([64])

H_CONV1 = tf.nn.relu(conv_layer(x_img,W1)+B1)
H_POOL1 = max_pool_2x2(H_CONV1)

#layer2
W2 = weight([5,5,1,128])
B2 = bias([128])

H_CONV2 = tf.nn.relu(conv_layer(H_POOL1,W2)+B2)
H_POOL2 = max_pool_2x2(H_CONV2)

# fc
W_f1 = weight([7*7*128,1024])
B_f1 = bias([1024])

h_pool_f1 = tf.reshape(H_POOL2,[-1,7*7*128])
h_f1 = tf.nn.relu(tf.matmul(h_pool_f1,W_f1)+ B_f1)

# fc2
keep_pro = tf.placeholder(tf.float32)
h_fc1_drop = tf.nn.dropout(h_f1,keep_pro)

W_f2 = weight([1024,10])
B_f2 =bias([10])


prediction = tf.nn.softmax(tf.matmul(h_fc1_drop,W_f2)+B_f2)
print('pre=',prediction.shape,',y=',y.shape)
# value_
cross_en = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y,logits=prediction))

train = tf.train.AdamOptimizer(1e-4).minimize(cross_en)

## 准确率
cor_prediction = tf.equal(tf.argmax(prediction,1),tf.argmax(y,1))
accuracy = tf.reduce_mean(tf.cast(cor_prediction,tf.float32))


with tf.Session() as sess:
  sess.run(tf.global_variables_initializer())
  for epoch in range(21):
    for batch in range(n):
      x_train,y_train = mnist.train.next_batch(batch_size)
      print(x_train.shape,y_train.shape)
      print('pre=',prediction.shape,',y=',y.shape)

      sess.run(train,feed_dict={x:x_train,y:y_train,keep_pro:0.7})

    acc = sess.run(accuracy,feed_dict={x:tf.mnist.test.images,y:tf.mnist.test.labels,keep_pro:1.0})
    print("Iter = "+str(epoch)+",test_accuracy = " +str(acc))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值