人工智能|TensorFlow前向传播实例

09/12 00:12

1 Relu函数模型

1.导入tensorflowtf、获取数据集

 import   tensorflow  as  tffrom     tensorflow  import  kerasfrom     tensorflow.keras  import  datasets(x,y),_ = datasets.mnist.load_data()

2.创建tensor

numpy转换为tensor，并且通过除以255来将x值从0~255调整为0~1

 x =  tf.convert_to_tensor(x,dtype=tf.float32) / 255.y =  tf.convert_to_tensor(y,dtype=tf.int32)print(tf.reduce_min(x),tf.reduce_max(x))   #查看x最小值、最大值print(tf.reduce_min(y),tf.reduce_max(y))   #查看y最小值、最大值

3.创建数据集

 train_db =  tf.data.Dataset.from_tensor_slices((x,y)).batch(128)train_iter = iter(train_db)  #迭代器，以便能够不停调用nextsample = next(train_iter)print('batch:',sample[0].shape,sample[1].shape)

4.定义参数与学习率

 w1 =  tf.Variable(tf.random.truncated_normal([784,256],stddev=0.1))   #stddev用来设置标准差b1 = tf.Variable(tf.zeros([256]))w2 =  tf.Variable(tf.random.truncated_normal([256,128],stddev=0.1))b2 = tf.Variable(tf.zeros([128]))w3 =  tf.Variable(tf.random.truncated_normal([128,10],stddev=0.1))b3 = tf.Variable(tf.zeros([10]))lr=1e-3

5.循环数据集

 for epoch in range(10):  # iterate db for 10     # 分批循环数据集     for step,(x,y) in enumerate(train_db):   # for every batch         # x:[128,28,28]        # y:[128]         # [b,28,28] => [b,28*28]         # 把 x 转换为[batch,784]         x = tf.reshape(x,[-1,28*28])          # tensor提供的自动求导         # 把训练过程放在with tf.GradientTape() as tape中，之后可以用tape.gradient()自动求得梯度         with tf.GradientTape() as tape:    # tf.Variable            # x:[b,28*28]            # h1 = x @ w1 + b1            # [b,784] @ [784,256] + [256]  => [b,256] + [256] => [b,256] + [256]            h1 = x @ w1 +  tf.broadcast_to(b1,[x.shape[0],256])            # 非线性激励             h1 = tf.nn.relu(h1)            #[b,256] => [b,128]            h2 = h1 @ w2 + b2            h2 = tf.nn.relu(h2)            # [b,128] => [b,10]            out = h2 @ w3 + b3             # compute loss            # out:[b,10]            # y:[b] => [b,10]            y_onehot = tf.one_hot(y,depth=10)             # mse = mean(sum(y-out)**2)            # [b,10]            loss = tf.square(y_onehot - out)            # mean:scalar            loss = tf.reduce_mean(loss)

6.传入损失函数

7.输入损失值

 if step % 100 == 0:             print(epoch,step,'loss:',float(loss))

0
0 收藏

0 评论
0 收藏
0