日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Tensorflow的基本运行方式--demo程序

發布時間:2025/5/22 编程问答 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Tensorflow的基本运行方式--demo程序 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1. Tensorflow的運行流程如下

  • 加載數據及定義超參數
  • 構建網絡
  • 訓練模型
  • 評估模型和進行預測

2. Tensorflow demo實現

demo如下:優化目標為:y=x2?0.5

# -*- coding: utf-8 -*- """ Created on Wed Apr 18 20:30:10 2018@author: spfhydiscription: tensorflow 的運行方式示例 """import tensorflow as tf import numpy as np#1. 生成輸入數據,學習方程為:y = x^2 - 0.5,構造滿足這個方程的一堆x,y,同時加入噪點 x_data = np.linspace(-1,1,30)[:,np.newaxis] #300*1的二維數組作為輸入noise = np.random.normal(0,0.05,x_data.shape)y_data = np.square(x_data) - 0.5 +noise#定義 x,y的占位符xs = tf.placeholder(tf.float32,[None,1]) ys = tf.placeholder(tf.float32,[None,1])def add_layer(inputs,in_size,out_size,activation_function =None):#構建權重:in_size*out_size大小的矩陣weights = tf.Variable(tf.random_normal([in_size,out_size]))#構建偏置:1*out_size的矩陣biases = tf.Variable(tf.zeros([1,out_size])+0.1)#矩陣相乘Wx_plus_b = tf.matmul(inputs,weights)+ biasesif activation_function is None:outputs = Wx_plus_belse:outputs = activation_function(Wx_plus_b)return outputs#構建隱匿層,假設隱匿層有20個神經元 h1 = add_layer(xs,1,20,activation_function=tf.nn.relu) #構建輸出層,假設輸出層和輸入層一樣,有1個神經元 prediction = add_layer(h1,20,1,activation_function=None)#構建損失函數:計算輸出層的預測值和真實值間的誤差,對二者差的方求和再取平均,得到損失 #函數,運用梯度下降法,以0.1的學習速率最小化損失:loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys -prediction),reduction_indices=[1])) #實現梯度下降算法的優化器 train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)init = tf.global_variables_initializer() sess = tf.Session() sess.run(init)for i in range(1000):sess.run(train_step,feed_dict={xs:x_data,ys:y_data})if i%50 == 0:print(sess.run(loss,feed_dict={xs:x_data,ys:y_data}))

本文時學習《TensorFlow 技術解析與實踐》的學習筆記,代碼摘抄自該書;

參考文獻:李嘉璇《TensorFlow 技術解析與實踐》

原文:https://blog.csdn.net/u010177286/article/details/79998193

總結

以上是生活随笔為你收集整理的Tensorflow的基本运行方式--demo程序的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。