import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt threshold = 1.0e-2
x1_data = np.random.randn(100).astype(np.float32)
x2_data = np.random.randn(100).astype(np.float32)
y_data = x1_data * 2 + x2_data * 3 + 1.5 weight1 = tf.Variable(1.)
weight2 = tf.Variable(1.)
bias = tf.Variable(1.)
x1_ = tf.placeholder(tf.float32)
x2_ = tf.placeholder(tf.float32)
y_ = tf.placeholder(tf.float32) y_model = tf.add(tf.add(tf.multiply(x1_, weight1), tf.multiply(x2_, weight2)),bias)
loss = tf.reduce_mean(tf.pow((y_model - y_),2)) train_op = tf.train.GradientDescentOptimizer(0.01).minimize(loss) sess = tf.Session()
init = tf.initialize_all_variables()
sess.run(init)
flag = 1
while(flag):
for (x,y) in zip(zip(x1_data, x2_data),y_data):
sess.run(train_op, feed_dict={x1_:x[0],x2_:x[1], y_:y})
if sess.run(loss, feed_dict={x1_:x[0],x2_:x[1], y_:y}) <= threshold:
flag = 0
print("weight1: " ,weight1.eval(sess),"weight2: " ,weight2.eval(sess),"bias: " ,bias.eval(sess))

import tensorflow as tf

matrix1 = tf.constant([[3., 3.]])
matrix2 = tf.constant([3., 3.]) sess = tf.Session() print(matrix1)
print(sess.run(matrix1))
print("----------------------")
print(matrix2)
print(sess.run(matrix2))

import tensorflow as tf

matrix1 = tf.constant([[3., 3.]])
matrix2 = tf.constant([3., 3.]) sess = tf.Session()
print(sess.run(tf.add(matrix1,matrix2)))

import tensorflow as tf

matrix1 = tf.constant([1, 2, 3, 4, 5, 6], shape=[2, 3])
matrix2 = tf.constant([1, 1, 1, 1, 1, 1], shape=[2, 3])
result1 = tf.multiply(matrix1,matrix2) sess = tf.Session()
print(sess.run(result1))

import tensorflow as tf

matrix1 = tf.constant([1, 2, 3, 4, 5, 6], shape=[2, 3])
matrix2 = tf.constant([1, 1, 1, 1, 1, 1], shape=[3, 2])
result2 = tf.matmul(matrix1,matrix2) sess = tf.Session()
print(sess.run(result2))

import tensorflow as tf

matrix1 = tf.Variable(tf.ones([3,3]))
matrix2 = tf.Variable(tf.zeros([3,3]))
result = tf.matmul(matrix1,matrix2) init=tf.initialize_all_variables()
sess = tf.Session()
sess.run(init) print(sess.run(matrix1))
print("--------------")
print(sess.run(matrix2))

import tensorflow as tf

a = tf.constant([[1,2],[3,4]])
matrix2 = tf.placeholder('float32',[2,2])
matrix1 = matrix2
sess = tf.Session()
a = sess.run(a)
print(sess.run(matrix1,feed_dict={matrix2:a}))
import tensorflow as tf
import numpy as np houses = 100
features = 2 #设计的模型为 2 * x1 + 3 * x2
x_data = np.zeros([houses,2])
for house in range(houses):
x_data[house,0] = np.round(np.random.uniform(50., 150.))
x_data[house,1] = np.round(np.random.uniform(3., 7.))
weights = np.array([[2.],[3.]])
y_data = np.dot(x_data,weights) x_data_ = tf.placeholder(tf.float32,[None,2])
y_data_ = tf.placeholder(tf.float32,[None,1])
weights_ = tf.Variable(np.ones([2,1]),dtype=tf.float32)
y_model = tf.matmul(x_data_,weights_) loss = tf.reduce_mean(tf.pow((y_model - y_data_),2))
train_op = tf.train.GradientDescentOptimizer(0.01).minimize(loss) sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init) for _ in range(10):
for x,y in zip(x_data,y_data):
z1 = x.reshape(1,2)
z2 = y.reshape(1,1)
sess.run(train_op,feed_dict={x_data_:z1,y_data_:z2})
print(weights_.eval(sess))

import tensorflow as tf
import numpy as np houses = 100
features = 2 #设计的模型为 2 * x1 + 3 * x2
x_data = np.zeros([100,2])
for house in range(houses):
x_data[house,0] = np.round(np.random.uniform(50., 150.))
x_data[house,1] = np.round(np.random.uniform(3., 7.))
weights = np.array([[2.],[3.]])
y_data = np.dot(x_data,weights) x_data_ = tf.placeholder(tf.float32,[None,2])
weights_ = tf.Variable(np.ones([2,1]),dtype=tf.float32)
y_model = tf.matmul(x_data_,weights_) loss = tf.reduce_mean(tf.pow((y_model - y_data),2))
train_op = tf.train.GradientDescentOptimizer(0.01).minimize(loss) sess = tf.Session()
init = tf.initialize_all_variables()
sess.run(init) for _ in range(20):
sess.run(train_op,feed_dict={x_data_:x_data})
print(weights_.eval(sess))

import numpy as np

a = np.array([[1,2,3],[4,5,6]])
print(a)
b = a.flatten()
print(b)

import numpy as np
import tensorflow as tf filename_queue = tf.train.string_input_producer(["D:\\F\\TensorFlow_deep_learn\\data\\cancer.txt"], shuffle=False)
reader = tf.TextLineReader()
key, value = reader.read(filename_queue)
record_defaults = [[1.0], [1.0], [1.0], [1.0], [1.0], [1.0], [1.0]]
col1, col2, col3, col4, col5 , col6 , col7 = tf.decode_csv(value,record_defaults=record_defaults)
print(col1)

import numpy as np
import tensorflow as tf def readFile(filename):
filename_queue = tf.train.string_input_producer(filename, shuffle=False)
reader = tf.TextLineReader()
key, value = reader.read(filename_queue)
record_defaults = [[1.0], [1.0], [1.0], [1.0], [1.0], [1.0], [1.0]]
col1, col2, col3, col4, col5 , col6 , col7 = tf.decode_csv(value,record_defaults=record_defaults)
label = tf.stack([col1,col2])
features = tf.stack([col3, col4, col5, col6, col7])
example_batch, label_batch = tf.train.shuffle_batch([features,label],batch_size=3, capacity=100, min_after_dequeue=10)
return example_batch,label_batch example_batch,label_batch = readFile(["D:\\F\\TensorFlow_deep_learn\\data\\cancer.txt"]) weight = tf.Variable(np.random.rand(5,1).astype(np.float32))
bias = tf.Variable(np.random.rand(3,1).astype(np.float32))
x_ = tf.placeholder(tf.float32, [None, 5])
y_model = tf.matmul(x_, weight) + bias
y = tf.placeholder(tf.float32, [None, 1]) loss = -tf.reduce_sum(y*tf.log(y_model))
train = tf.train.GradientDescentOptimizer(0.1).minimize(loss)
init = tf.initialize_all_variables() with tf.Session() as sess:
sess.run(init)
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
flag = 1
while(flag):
e_val, l_val = sess.run([example_batch, label_batch])
sess.run(train, feed_dict={x_: e_val, y: l_val})
if sess.run(loss,{x_: e_val, y: l_val}) <= 1:
flag = 0
print(sess.run(weight))

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

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

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

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

    # coding: utf-8 import time import numpy as np import tensorflow as tf import _pickle as pickle impo ...

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

    import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data import time # 声明输 ...

  4. 吴裕雄 python深度学习与实践(16)

    import struct import numpy as np import matplotlib.pyplot as plt dateMat = np.ones((7,7)) kernel = n ...

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

    import numpy as np import matplotlib.pyplot as plt x_data = np.random.randn(10) print(x_data) y_data ...

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

    import tensorflow as tf q = tf.FIFOQueue(,"float32") counter = tf.Variable(0.0) add_op = t ...

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

    import numpy as np from matplotlib import pyplot as plt A = np.array([[5],[4]]) C = np.array([[4],[6 ...

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

    import tensorflow as tf input1 = tf.constant(1) print(input1) input2 = tf.Variable(2,tf.int32) print ...

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

    import numpy as np import tensorflow as tf inputX = np.random.rand(100) inputY = np.multiply(3,input ...

随机推荐

  1. jQuery-2.DOM---节点的删除

    DOM节点删除之empty()的基本用法 要移除页面上节点是开发者常见的操作,jQuery提供了几种不同的方法用来处理这个问题,这里我们开仔细了解下empty方法 empty 顾名思义,清空方法,但是 ...

  2. Javascript 2.8

    声明函数声明参数 function multiply(A,B,...N){}; 用return可以返回一个值/字符串/数组/布尔值 变量命名的Camel记号:从第二个单词开始把每个单词的首字母大写,其 ...

  3. Tarjan求割点&桥

    概念 1.桥:是存在于无向图中的这样的一条边,如果去掉这一条边,那么整张无向图会分为两部分,这样的一条边称为桥无向连通图中,如果删除某边后,图变成不连通,则称该边为桥. 2.割点:无向连通图中,如果删 ...

  4. Python(一)—— 控制流:if & for & while

    基操 编程语言类 编译型 程序在执行之前需要一个专门的编译过程,把程序编译成 为机器语言的文件,运行时不需要重新翻译,直接使用编译的结果就行了.程序执行效率高,依赖编译器,跨平台性差些.缺点:编译之后 ...

  5. randrange()和random() 函数

    描述 randrange() 方法返回指定递增基数集合中的一个随机数,基数缺省值为1. 语法 以下是 randrange() 方法的语法: impot random random.randrange ...

  6. BIO、NIO实战

    BIO BIO:blocking IO,分别写一个服务端和客户端交互的C/S实例.服务器端: import java.io.BufferedReader; import java.io.IOExcep ...

  7. 图像小波变换去噪——MATLAB实现

    clear; [A,map]=imread('C:\Users\wangd\Documents\MATLAB\1.jpg'); X=rgb2gray(A); %画出原始图像 subplot(,,);i ...

  8. C语言小程序:除去字符串中间不需要的字符(从小引发大思考)

    #include <stdio.h>#include <conio.h> void fun(char *a, char *h, char *p){ char b[81]; ch ...

  9. 《精通python网络爬虫》笔记

    <精通python网络爬虫>韦玮 著 目录结构 第一章 什么是网络爬虫 第二章 爬虫技能概览 第三章 爬虫实现原理与实现技术 第四章 Urllib库与URLError异常处理 第五章 正则 ...

  10. linux中telnet后退出连接窗口的方法?

    linux中telnet后退出连接窗口 [root@a cron]# telnet www.baidu.com 80Trying 115.239.211.112...Connected to www. ...