吴裕雄 python深度学习与实践(10)

import tensorflow as tf

input1 = tf.constant(1)
print(input1)

input2 = tf.Variable(2,tf.int32)
print(input2)

input2 = input1
sess = tf.Session()
print(sess.run(input2))

import tensorflow as tf

input1 = tf.placeholder(tf.int32)
input2 = tf.placeholder(tf.int32)

output = tf.add(input1, input2)

sess = tf.Session()
print(sess.run(output, feed_dict={input1:[1], input2:[2]}))

import numpy as np
import tensorflow as tf

"""
这里是一个非常好的大数据验证结果,随着数据量的上升,集合的结果也越来越接近真实值,
这也是反馈神经网络的一个比较好的应用
这里不是很需要各种激励函数
而对于dropout,这里可以看到加上dropout,loss的值更快。
随着数据量的上升,结果就更加接近于真实值。
"""

inputX = np.random.rand(3000,1)
noise = np.random.normal(0, 0.05, inputX.shape)
outputY = inputX * 4 + 1 + noise

#这里是第一层
weight1 = tf.Variable(np.random.rand(inputX.shape[1],4))
bias1 = tf.Variable(np.random.rand(inputX.shape[1],4))
x1 = tf.placeholder(tf.float64, [None, 1])
y1_ = tf.matmul(x1, weight1) + bias1

y = tf.placeholder(tf.float64, [None, 1])
loss = tf.reduce_mean(tf.reduce_sum(tf.square((y1_ - y)), reduction_indices=[1]))
train = tf.train.GradientDescentOptimizer(0.25).minimize(loss)  # 选择梯度下降法

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

for i in range(1000):
    sess.run(train, feed_dict={x1: inputX, y: outputY})

print(weight1.eval(sess))
print("---------------------")
print(bias1.eval(sess))
print("------------------结果是------------------")

x_data = np.matrix([[1.],[2.],[3.]])
print(sess.run(y1_,feed_dict={x1: x_data}))

import numpy as np

aa = np.random.rand(5,4)
print(aa)
print(np.shape(aa))

import tensorflow as tf
import numpy as np

"""
这里是一个非常好的大数据验证结果,随着数据量的上升,集合的结果也越来越接近真实值,
这也是反馈神经网络的一个比较好的应用
这里不是很需要各种激励函数
而对于dropout,这里可以看到加上dropout,loss的值更快。
随着数据量的上升,结果就更加接近于真实值。
"""

inputX = np.random.rand(3000,1)
noise = np.random.normal(0, 0.05, inputX.shape)
outputY = inputX * 4 + 1 + noise

#这里是第一层
weight1 = tf.Variable(np.random.rand(inputX.shape[1],4))
bias1 = tf.Variable(np.random.rand(inputX.shape[1],4))
x1 = tf.placeholder(tf.float64, [None, 1])
y1_ = tf.matmul(x1, weight1) + bias1
#这里是第二层
weight2 = tf.Variable(np.random.rand(4,1))
bias2 = tf.Variable(np.random.rand(inputX.shape[1],1))
y2_ = tf.matmul(y1_, weight2) + bias2

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

loss = tf.reduce_mean(tf.reduce_sum(tf.square((y2_ - y)), reduction_indices=[1]))
train = tf.train.GradientDescentOptimizer(0.25).minimize(loss)  # 选择梯度下降法

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

for i in range(1000):
    sess.run(train, feed_dict={x1: inputX, y: outputY})

print(weight1.eval(sess))
print("---------------------")
print(weight2.eval(sess))
print("---------------------")
print(bias1.eval(sess))
print("---------------------")
print(bias2.eval(sess))
print("------------------结果是------------------")

x_data = np.matrix([[1.],[2.],[3.]])
print(sess.run(y2_,feed_dict={x1: x_data}))

原文地址:https://www.cnblogs.com/tszr/p/10356471.html

时间: 2024-08-30 18:14:48

吴裕雄 python深度学习与实践(10)的相关文章

吴裕雄 python深度学习与实践(5)

import numpy as np data = np.mat([[1,200,105,3,False], [2,165,80,2,False], [3,184.5,120,2,False], [4,116,70.8,1,False], [5,270,150,4,True]]) row = 0 for line in data: row += 1 print(row) print(data.size) import numpy as np data = np.mat([[1,200,105,3

吴裕雄 python深度学习与实践(6)

from pylab import * import pandas as pd import matplotlib.pyplot as plot import numpy as np filePath = ("G:\\MyLearning\\TensorFlow_deep_learn\\data\\dataTest.csv") dataFile = pd.read_csv(filePath,header=None, prefix="V") summary = dat

吴裕雄 python深度学习与实践(7)

import cv2 import numpy as np img = np.mat(np.zeros((300,300))) cv2.imshow("test",img) cv2.waitKey(0) import cv2 import numpy as np img = np.mat(np.zeros((300,300),dtype=np.uint8)) cv2.imshow("test",img) cv2.waitKey(0) import cv2 impor

吴裕雄 python深度学习与实践(11)

import numpy as np from matplotlib import pyplot as plt A = np.array([[5],[4]]) C = np.array([[4],[6]]) B = A.T.dot(C) AA = np.linalg.inv(A.T.dot(A)) l=AA.dot(B) P=A.dot(l) x=np.linspace(-2,2,10) x.shape=(1,10) xx=A.dot(x) fig = plt.figure() ax= fig.

吴裕雄 python深度学习与实践(12)

import tensorflow as tf q = tf.FIFOQueue(1000,"float32") counter = tf.Variable(0.0) add_op = tf.assign_add(counter, tf.constant(1.0)) enqueueData_op = q.enqueue(counter) sess = tf.Session() qr = tf.train.QueueRunner(q, enqueue_ops=[add_op, enque

吴裕雄 python深度学习与实践(15)

import tensorflow as tf import tensorflow.examples.tutorials.mnist.input_data as input_data mnist = input_data.read_data_sets("D:\\F\\TensorFlow_deep_learn\\MNIST\\", one_hot=True) x_data = tf.placeholder("float32", [None, 784]) weight

吴裕雄 python深度学习与实践(17)

import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data import time # 声明输入图片数据,类别 x = tf.placeholder('float', [None, 784]) y_ = tf.placeholder('float', [None, 10]) # 输入图片数据转化 x_image = tf.reshape(x, [-1, 28, 28, 1]) #第一层卷积层

吴裕雄 python深度学习与实践(1)

#coding = utf8 import threading,time count = 0 class MyThread(threading.Thread): def __init__(self,threadName): super(MyThread,self).__init__(name = threadName) def run(self): global count for i in range(100): count = count + 1 time.sleep(0.3) print(

吴裕雄 python深度学习与实践(2)

#coding = utf8 import threading,time,random count = 0 class MyThread (threading.Thread): def __init__(self,lock,threadName): super(MyThread,self).__init__(name = threadName) self.lock = lock def run(self): global count self.lock.acquire() for i in ra