吴裕雄 python深度学习与实践(13)
import numpy as np
import matplotlib.pyplot as plt x_data = np.random.randn(10)
print(x_data)
y_data = x_data * 0.3 + 0.15
print(y_data) plt.plot(x_data,y_data)
plt.scatter(x_data,y_data,c="r")
plt.show()
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt x_data = np.random.randn(10)
y_data = x_data * 0.3 + 0.15 weight = tf.Variable(0.5)
bias = tf.Variable(0.0)
y_model = weight * x_data + bias loss = 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(200):
sess.run(train_op)
print(weight.eval(sess),bias.eval(sess)) plt.plot(x_data, y_data, 'ro', label='Original data')
plt.plot(x_data, sess.run(weight) * x_data + sess.run(bias), label='Fitted line')
plt.legend()
plt.show()
.........................
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt x_data = np.random.randn(10)#.astype(np.float32)
y_data = x_data * 0.3 + 0.1 weight = tf.Variable(0.5)
bias = tf.Variable(0.0)
x_ = tf.placeholder(tf.float32)
y_ = tf.placeholder(tf.float32)
y_model = weight * x_ + bias loss = 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) for _ in range(5):
for (x,y) in zip(x_data,y_data):
sess.run(train_op,feed_dict={x_:x,y_:y})
print("weighe: " ,weight.eval(sess)," | bias: ",bias.eval(sess)) plt.plot(x_data, y_data, 'ro', label='Original data')
plt.plot(x_data, sess.run(weight) * (x_data) + sess.run(bias), label='Fitted line')
plt.legend()
plt.show()
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt threshold = 1.0e-2
x_data = np.random.randn(10).astype(np.float32)
y_data = x_data * 3 + 1 weight = tf.Variable(1.)
bias = tf.Variable(1.)
x_ = tf.placeholder(tf.float32)
y_ = tf.placeholder(tf.float32)
y_model = tf.add(tf.multiply(x_, weight), 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(x_data,y_data):
sess.run(train_op,feed_dict={x_:x,y_:y})
print(weight.eval(sess), bias.eval(sess))
if(sess.run(loss,feed_dict={x_:x_data,y_:y_data}) <= threshold):
flag = 0 plt.plot(x_data, y_data, 'ro', label='Original data')
plt.plot(x_data, sess.run(weight) * (x_data) + sess.run(bias), label='Fitted line')
plt.legend()
plt.show()
......................................................
#coding:utf-8 import numpy as np a = np.array([1,2,3])
b = np.array([7,8])
res = np.meshgrid(a,b)
for i in res:
print(i)
import numpy as np
import matplotlib.pyplot as plt x = np.array([[0, 1, 2], [0, 1, 2]])
y = np.array([[0, 0, 0], [1, 1, 1]]) plt.plot(x, y,color='red',marker='.', linestyle='--')
plt.grid(True)
plt.show()
plt.plot(x, y, marker='.',markersize=10,linestyle='-.')
plt.grid(True)
plt.show()
import numpy as np
import matplotlib.pyplot as plt x = np.array([[0, 1, 2, 3],
[0, 1, 2, 3],
[0, 1, 2, 3],
[0, 1, 2, 3]])
y = np.array([[0, 0, 0, 0],
[1, 1, 1, 1],
[2, 2, 2, 2],
[3, 3, 3, 3]]) plt.plot(x, y,marker='.', markersize=10,linestyle='-.')
plt.grid(True)
plt.show()
import numpy as np
import matplotlib.pyplot as plt x = np.array([0, 1, 2])
y = np.array([0, 1])
X, Y = np.meshgrid(x, y)
print(X)
print(Y) plt.plot(X, Y,color='red', marker='.', linestyle='')
plt.grid(True)
plt.show()
import numpy as np
import matplotlib.pyplot as plt x = np.linspace(0,1000,20)
y = np.linspace(0,500,20) X,Y = np.meshgrid(x, y) plt.plot(X, Y,color='limegreen', marker='.',linestyle='')
plt.grid(True)
plt.show()
import numpy as np
import matplotlib.pyplot as plt from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.ticker import LinearLocator, FormatStrFormatter fig = plt.figure(figsize=(16,12))
ax = fig.gca(projection="3d") #准备数据
x = np.arange(-5, 5, 0.25) #生成[-5,5] 间隔0.25的数列,间隔越小,曲面越平滑
y = np.arange(-5, 5, 0.25)
x, y = np.meshgrid(x,y) #格点矩阵,原来的x行向量向下复制len(y)此形成
r = np.sqrt(x ** 2 + y ** 2)
z = np.sin(r) surf = ax.plot_surface(x, y, z, cmap=cm.coolwarm) #cmap指color map #自定义z轴
ax.set_zlim(-1, 1)
ax.zaxis.set_major_locator(LinearLocator(20)) #z轴网格线的疏密,刻度的疏密,20表示刻度的个数
ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f')) #将z的value子符串转为float,保留2位小数 #设置坐标轴的label和标题
ax.set_xlabel('x', size=15)
ax.set_ylabel('y', size=15)
ax.set_zlabel('z', size=15)
ax.set_title("Surface plot", weight='bold', size=20) #添加右侧的色卡条
fig.colorbar(surf, shrink=0.6, aspect=8) #shrink表示整体收缩比例,aspect仅对bar的宽度有影响,
# aspect值越大,bar越窄 plt.show()
import numpy as np
import matplotlib.pyplot as plt n=1024
X=np.random.normal(0,1,n)
Y=np.random.normal(0,1,n)
T=np.arctan2(Y,X)#返回给定的 X 及 Y 坐标值的反正切值。 plt.scatter(X,Y,s=75,c=T,alpha=0.5) plt.xlim(-1.5,1.5)
plt.xticks(())
plt.ylim(-1.5,1.5)
plt.yticks(()) plt.show()
import matplotlib.pyplot as plt
import numpy as np n=12
X=np.arange(n)
Y1=(1 - X / float(n)) * np.random.uniform(0.5,1.0,n)
Y2=(1 - X / float(n)) * np.random.uniform(0.5,1.0,n) plt.bar(X,+Y1,facecolor='#9999ff',edgecolor='white')
plt.bar(X,-Y2,facecolor='#ff9999',edgecolor='white') for x,y in zip(X,Y1):
plt.text(x+0.4,y+0.05,'%.2f' % y,ha='center',va='bottom') for x,y in zip(X,Y2):
plt.text(x+0.4,-y-0.05,'%.2f' % y,ha='center',va='bottom') plt.xlim(-1,n)
plt.xticks(())
plt.ylim(-1.25,1.25)
plt.yticks(()) plt.show()
import numpy as np
import matplotlib.pyplot as plt def f(x,y):
return (1 - x/2 + x**5 + y**3) * np.exp(-x**2,-y**2) n=256
x=np.linspace(-3,3,n)
y=np.linspace(-3,3,n)
X,Y=np.meshgrid(x,y) plt.contourf(X,Y,f(X,Y),8,alpha=0.75,cmap=plt.cm.hot)
C=plt.contour(X,Y,f(X,Y),8,colors='black',linewidth=0.5) plt.clabel(C,inline=True,fontsize=10)
plt.xticks(())
plt.yticks(())
plt.show()
import numpy as np
import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D fig=plt.figure()
ax=Axes3D(fig) X=np.arange(-4,4,0.25)
Y=np.arange(-4,4,0.25)
X,Y=np.meshgrid(X,Y)
R=np.sqrt(X ** 2 + Y ** 2)
Z=np.sin(R) ax.plot_surface(X,Y,Z,rstride=1,cstride=1,cmap=plt.get_cmap('rainbow'))
ax.contourf(X,Y,Z,zdir='z',offset=-2,cmap=plt.get_cmap('rainbow')) plt.show()
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D 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 fig = plt.figure()
ax = Axes3D(fig)
X, Y = np.meshgrid(x1_data, x2_data)
Z = sess.run(weight1) * (X) + sess.run(weight2) * (Y) + sess.run(bias)
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=plt.cm.hot)
ax.contourf(X, Y, Z, zdir='z', offset=-1, cmap=plt.cm.hot)
ax.set_zlim(-1, 1)
plt.show()
吴裕雄 python深度学习与实践(13)的更多相关文章
- 吴裕雄 python深度学习与实践(6)
from pylab import * import pandas as pd import matplotlib.pyplot as plot import numpy as np filePath ...
- 吴裕雄 python深度学习与实践(18)
# coding: utf-8 import time import numpy as np import tensorflow as tf import _pickle as pickle impo ...
- 吴裕雄 python深度学习与实践(17)
import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data import time # 声明输 ...
- 吴裕雄 python深度学习与实践(16)
import struct import numpy as np import matplotlib.pyplot as plt dateMat = np.ones((7,7)) kernel = n ...
- 吴裕雄 python深度学习与实践(15)
import tensorflow as tf import tensorflow.examples.tutorials.mnist.input_data as input_data mnist = ...
- 吴裕雄 python深度学习与实践(14)
import numpy as np import tensorflow as tf import matplotlib.pyplot as plt threshold = 1.0e-2 x1_dat ...
- 吴裕雄 python深度学习与实践(12)
import tensorflow as tf q = tf.FIFOQueue(,"float32") counter = tf.Variable(0.0) add_op = t ...
- 吴裕雄 python深度学习与实践(11)
import numpy as np from matplotlib import pyplot as plt A = np.array([[5],[4]]) C = np.array([[4],[6 ...
- 吴裕雄 python深度学习与实践(10)
import tensorflow as tf input1 = tf.constant(1) print(input1) input2 = tf.Variable(2,tf.int32) print ...
随机推荐
- 我在MySQL免安装版使用过程中遇到的问题记录
我的MySQL版本为:mysql-5.7.16-winx64 安装时间为:2016年5月10号 由于是免安装版,下载好压缩文件之后解压到特定目录下,再打开命令行运行几行命令即可. 在一次操作中,发现无 ...
- JAVA高级篇(二、JVM内存模型、内存管理之第一篇)
JVM内存结构如 Java堆(Heap),是Java虚拟机所管理的内存中最大的一块.Java堆是被所有线程共享的一块内存区域,在虚拟机启动时创建.此内存区域的唯一目的就是存放对象实例,几乎所有的对象实 ...
- 《JavaScript 设计模式与开发实战》第一部分(1、2、3章)笔记
第1章:面向对象的JavaScript 动态类型和鸭子类型 编程语言按照数据类型大体可以分为两类: ① 静态类型语言:在编译时便已确定变量的类型. ② 动态类型语言:变量类型要到程序运行的时候,待变量 ...
- Semantic Compositionality through Recursive Matrix-Vector Spaces-paper
Semantic Compositionality through Recursive Matrix-Vector Spaces 作者信息:Richard Socher Brody Huval Chr ...
- Selenium - 搭建环境
1. 在Python中安装第三方库 1)安装Selenium 通过pip安装 2). 下载geckodriverckod 从selenium3开始,webdriver/firefox/webdri ...
- 超级简单的数据压缩算法—LZW算法
1. 前文回顾 在字符串算法—数据压缩中,我们介绍了哈夫曼压缩算法(Huffman compression),本文将介绍LZW算法. 2. LZW算法 这个算法很简单,为了方便讲述,我们将采用16进制 ...
- Google - chanceToLose24Game
/* 一个类似24点的游戏,假设牌桌上有无数张1-10的牌,然后你手上的牌的总和是k,现在你可以随机到牌桌上抽牌加到总和里,如果你手上牌的总和在20-25之间就是win,如果总和超过25就是lose, ...
- PythonStudy——名称空间 Name space
# 名称空间:就是名字与地址的对应关系,可以被Python解释器遍历查找,采用堆栈存储方式 # 三种名称空间# Built-in:内置名称空间:系统级,一个:随解释器执行而产生,解释器停止而销毁# G ...
- PythonStudy——列表操作 List operatio
# 1.列表的增删改查 ls = [1, 2, 3] # 查 print(ls) print(ls[1]) # 增 ls.append(0) # 末尾增 print(ls) ls.insert(1, ...
- Linux双线双网卡双IP双网关设置方法
机房上架了一台测试机,系统是Ubuntu 9.04 X64的系统,母机IBM X336机器.用户需求是双线,故采用一个网卡配置电信地址,另一个网卡配置联通地址,安装好系统后配置好IP发现联通地址和电信 ...