莫烦tensorflow(1)-训练线性函数模型

import tensorflow as tf
import numpy as np

#create data
x_data = np.random.rand(100).astype(np.float32)
y_data = x_data*0.1+0.3

####create tensorflow structure start###
Weights = tf.Variable(tf.random_uniform([1],-1.0,1.0))
biases = tf.Variable(tf.zeros([1]))

y = Weights*x_data+biases

loss = tf.reduce_mean(tf.square(y-y_data))
optimizer = tf.train.GradientDescentOptimizer(0.5)
train = optimizer.minimize(loss)

init = tf.initialize_all_variables()
####create tensorflow structure end###

sess = tf.Session()
sess.run(init)

for step in range(201):
sess.run(train)
if step %20 == 0:
print(step,sess.run(Weights),sess.run(biases))

时间: 2024-11-09 09:41:26

莫烦tensorflow(1)-训练线性函数模型的相关文章

莫烦TENSORFLOW(4)-placeholder

import tensorflow as tf input1 = tf.placeholder(tf.float32)input2 = tf.placeholder(tf.float32) output = tf.multiply(input1,input2) with tf.Session() as sess: print(sess.run(output,feed_dict={input1:[7.],input2:[2.0]}))

莫烦TENSORFLOW(6)-tensorboard

import tensorflow as tfimport numpy as np def add_layer(inputs,in_size,out_size,n_layer,activation_function=None): # add one more layer and return the output of this layer layer_name = 'layer%s' % n_layer with tf.name_scope('layer'): with tf.name_sco

莫烦TENSORFLOW(5)

import tensorflow as tfimport numpy as npimport matplotlib.pyplot as plt def add_layer(inputs,in_size,out_size,activation_function=None): Weights = tf.Variable(tf.random_normal([in_size,out_size])) biases = tf.Variable(tf.zeros([1,out_size]) + 0.1) W

莫烦TENSORFLOW(7)-mnist

import tensorflow as tffrom tensorflow.examples.tutorials.mnist import input_data#number 1 to 10 datamnist = input_data.read_data_sets('MNIST_data',one_hot=True) def add_layer(inputs,in_size,out_size,activation_function=None): Weights = tf.Variable(t

莫烦TENSORFLOW(3)-Variable

import tensorflow as tf state = tf.Variable(0,name='counter') one = tf.constant(1) new_value = tf.add(state,one)update = tf.assign(state,new_value) init = tf.initialize_all_variables()#must have if define variable with tf.Session() as sess: sess.run(

莫烦tensorflow(9)-Save&Restore

import tensorflow as tfimport numpy as np ##save to file#rember to define the same dtype and shape when restore# W = tf.Variable([[1,2,3],[3,4,5]],dtype=tf.float32,name='Weights')# b = tf.Variable([[1,2,3]],dtype=tf.float32,name='biases') # init = tf

莫烦tensorflow(8)-CNN

import tensorflow as tffrom tensorflow.examples.tutorials.mnist import input_data#number 1 to 10 datamnist = input_data.read_data_sets('MNIST_data',one_hot=True) def compute_accuracy(v_xs,v_ys): global prediction y_pre = sess.run(prediction,feed_dict

莫烦tensorflow(2)-Session

import os os.environ['TF_CPP_MIN_LOG_LEVEL']='2' import tensorflow as tfmatrix1 = tf.constant([[3,3]])matrix2 = tf.constant([[2],[2]])protuct = tf.matmul(matrix1,matrix2) # sess = tf.Session()# result = sess.run(protuct) # print(result)# sess.close()

tensorflow 莫烦教程

1,感谢莫烦 2,第一个实例:用tf拟合线性函数 import tensorflow as tf import numpy as np # create data x_data = np.random.rand(100).astype(np.float32) y_data = x_data*0.1 + 0.3 #先创建我们的线性函数目标 #搭建模型 Weights = tf.Variable(tf.random_uniform([1], -1.0, 1.0)) biases = tf.Varia