tensorflow安装(windows系统中的anaconda)
1:点击anaconda prompt,输入后键入命令
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
2:键入命令
conda config --set show_channel_urls yes
3.创造环境(注意,如果你只想base环境安装tensorflow可能不成功,需要创造另一个环境) conda create -n py35 python=3.5 4:conda activate py35进入环境,键入命令
conda install tensorflow
到此就在py安装在35个环境中tensorflow,不过这个版本还需要自己去解决 上面这个tensorflow版本太低了,很多库都不能用,最新版本可以重新安装。tensorflow,这里用的是3.8版本的python,因此需要删除原来的虚拟环境:使用命令conda remove -n py35 -all 重新安装tensorflow 直接配置虚拟环境后,pip install tensorflow 最新版本将自动安装。
这个代码是我两年前写的。当时代码功底很差,所以有一些代码。bug而且解泊松方程的效果不是很好,读者可以尝试修改代码以提高数值精度
code1
import numpy as np import tensorflow as tf import matplotlib.pyplot as plt tf.compat.v1.disable_eager_execution() import time tic = time.time() h1 = 0.1 h2 = 0.05 M = int(1/h1) 1 N = int(1/h2) 1 x_train = np.linspace(0,1,M) y_train = np.linspace(0,1,N) matr = np.zeros([M,N,2])#将所有网格存储在
矩阵中matrix在上面,每行代表一个网格点的坐标 for i in range(M): for j in range(N): matr[i,j,0] = x_train[i] matr[i,j,1] = y_train[j] ma = matr.reshape(-1,2) print(ma.shape) ma_in = matr[1:-1,1:-1].reshape(-1,2)#将所有网格点存储在矩阵matrix上,每一行代表一个网格点的坐标 print(ma_in.shape) ma_b = np.concatenate([matr[0],matr[-1],matr[:,0][1:-1],matr[:,-1][1:-1]],0) print(ma_b.shape) def f(x,y):#微分方程的右边函数f return - (2*np.pi**2)*np.exp(np.pi*(x + y))*np.sin(np.pi*(x + y)) def u_accuracy(x,y): return np.exp(np.pi*(x + y))*np.sin(np.pi*x)*np.sin(np.pi*y) def u_boundary(x,y): return 0*x*y right_in = f(ma_in[:,0],ma_in[:,1]).reshape(-1,1) right_b = u_boundary(ma_b[:,0],ma_b[:,1]).reshape(-1,1) print(right_in.shape,right_b.shape) ma_min = np.array([[0,0]]) ma_max = np.array([[1,1]]) np.random.seed(1234) tf.compat.v1.set_random_seed(1234) #定义tensorflow框架 prob_tf = tf.compat.v1.placeholder(tf.float32) x_tf = tf.compat.v1.placeholder(tf.float32,shape = [None,2]) x_in_tf = tf.compat.v1.placeholder(tf.float32,shape = [None,2]) x_b_tf = tf.compat.v1.placeholder(tf.float32,shape = [None,2]) right_in_tf = tf.compat.v1.placeholder(tf.float32,shape = [None,1]) right_b_tf = tf.compat.v1.placeholder(tf.float32,shape = [None,1]) x_min_tf = tf.compat.v1.placeholder(tf.float32,shape = [1,2]) x_max_tf = tf.compat.v1.placeholder(tf.float32,shape = [1,2]) layers = [2,10,10,1] H = 2*(x_tf - x_min_tf)/(x_max_tf - x_min_tf + 1e-7) - 1 H_in = 2*(x_in_tf - x_min_tf)/(x_max_tf - x_min_tf + 1e-7) - 1 H_b = 2*(x_b_tf - x_min_tf)/(x_max_tf - x_min_tf + 1e-7) - 1 #定义权重和偏置 def w_init(in_dim,out_dim): w_std = np.sqrt(2/(in_dim + out_dim)) return tf.Variable(tf.random.truncated_normal([in_dim, out_dim], stddev = w_std), dtype=tf.float32) weights = [] biases = [] num_layers = len(layers) for i in range(0,num_layers - 1): w = w_init(in_dim = layers[i],out_dim = layers[i + 1]) b = tf.Variable(tf.random.truncated_normal([1,layers[i + 1]], dtype = tf.float32), dtype=tf.float32) weights.append(w) biases.append(b) #输出近似解 num_layers = len(weights)#比layers长度少1 for i in range(0,num_layers - 1): w = weights[i] b = biases[i] E = tf.eye(tf.shape(w)[0],tf.shape(w)[1]) tf.nn.dropout(w,rate = prob_tf) H_in = tf.tanh(tf.add(tf.matmul(H_in,w), b)) + tf.matmul(H_in,E) H_b = tf.tanh(tf.add(tf.matmul(H_b,w), b)) + tf.matmul(H_b,E) H = tf.tanh(tf.add(tf.matmul(H,w), b)) + tf.matmul(H,E) W = weights[-1] b = biases[-1] u_in = tf.add(tf.matmul(H_in,W),b) u_b = tf.add(tf.matmul(H_b,W),b) u = tf.add(tf.matmul(H,W),b) #定义损失函数 u_x = tf.gradients(u_in,x_in_tf)[0][:,0] u_y = tf.gradients(u_in,x_in_tf)[0][:,1] u_xx = tf.gradients(u_x,x_in_tf)[0][:,0] u_yy = tf.gradients(u_y,x_in_tf)[0][:,1] loss_in = 0.5*tf.reduce_mean(tf.square(u_x) + tf.square(u_y) - 2*u_in*right_in_tf) #loss_in = 0.5*tf.reduce_mean((-u_xx - u_yy - 2*right_in_tf)*u_in) loss_b = tf.reduce_mean(tf.square(u_b - right_b_tf)) belta = 5e3 loss = tf.add(loss_in,belta*loss_b) #初始化 def plot_curve(data):#用于损失函数训练过程的可视化 fig = plt.figure(num = 1,figsize = (4,3),dpi = None,facecolor = None,edgecolor = None,frameon = True)#编号,宽和高,分辨率,背景颜色,边框颜色,是否显示边框 plt.plot(range(len(data)),data,color = 'blue') plt.legend(['value'],loc = 'upper right') plt.xlabel('x') plt.ylabel('y') plt.show() lr = 5*1e-3 optim = tf.compat.v1.train.AdamOptimizer(learning_rate = lr) trainer = optim.minimize(loss) sess = tf.compat.v1.Session() init = tf.compat.v1.global_variables_initializer() sess.run(init) train_loss = [] step = [] error = [] train_step = 6000 for i in range(train_step): batch = 19 st = i*batch%len(ma_in) end = st+ batch feed = { prob_tf:0.5,x_tf:ma,x_in_tf:ma_in[st:end],x_b_tf:ma_b,right_in_tf:right_in[st:end],right_b_tf:right_b,x_min_tf:ma_min,x_max_tf:ma_max} sess.run(trainer,feed_dict = feed) if i%600 == 0: print('train_step = {},loss = {}'.format(i,sess.run(loss,feed_dict = feed))) train_loss.append(sess.run(loss,feed_dict = feed)) feed_val = { x_tf:ma,x_in_tf:ma_in,x_b_tf:ma_b,right_in_tf:right_in,right_b_tf:right_b,x_min_tf:ma_min,x_max_tf:ma_max} u_pred = sess.run(u,feed_dict = feed_val).reshape(M,N) x,y = np.meshgrid(x_train,y_train) error_square = ((u_pred - u_accuracy(x,y).T)**2).sum()/(u_accuracy(x,y)**2 + 1e-7).sum() error_step = np.sqrt(error_square) error.append(error_step) step.append(i) print(error_step) toc = time.time() plot_curve(train_loss) print(toc - tic) feed = { prob_tf:0.5,x_tf:ma,x_in_tf:ma_in,x_b_tf:ma_b,right_in_tf:right_in,right_b_tf:right_b,x_min_tf:ma_min,x_max_tf:ma_max} u_pred = sess.run(u,feed_dict = feed).reshape(M,N) print(type(u_pred),u_pred.shape) x,y = np.meshgrid(x_train,y_train) print(type(u_accuracy(x,y)),u_accuracy(x,y).shape) error_square = ((u_pred - u_accuracy(x,y).T)**2).sum()/(u_accuracy(x,y)**2 + 1e-7).sum() error = np.sqrt(error_square) print(error) plt.subplot(2,1,1) x,y = np.meshgrid(x_train,y_train) plt.contourf(x,y,u_accuracy(x,y),40,cmap = 'Blues') plt.colorbar() plt.xlabel('x') plt.ylabel('y') plt.title('accuracy solution') plt.subplot(2,1,2) x,y = np.meshgrid(x_train,y_train) plt.contourf(x,y,u_pred.T,40,cmap = 'Blues') plt.colorbar() plt.xlabel('x') plt.ylabel('y') plt.title('numerical solution')
code2
import tensorflow as tf import numpy as np import matplotlib.pyplot as plt import time np.random.seed(1234) tf.compat.v1.set_random_seed(1234) tf.compat.v1.disable_eager_execution() def f(x,y):#微分方程的右边函数f return - (2*np.pi**2)*np.exp(np.pi*(x + y))*np.sin(np.pi*(x + y)) def u_accuracy(x,y):#定义精确解 return np.exp(np.pi*(x + y))*np.sin(np.pi*x)*np.sin(np.pi*y) def u_boundary(x,y):#定义边界函数 return 0*x*y #定义内部点 class INSET():#定义训练集,包含区域内部点 def __init__(self): self.dim = 2 self.xa,self.xb,self.ya,self.yb = 0,1,0,1 self.area = (self.xb - self.xa)*(self.yb - self.ya) self.nx,self.ny = 20,30 self.hx = (self.xb - self.xa)/self.nx self.hy = (self.yb - self.ya)/self.ny self.size = self.nx*self.ny self.X = np.zeros([self.size,self.dim]) for i in range(self.nx): for j in range(self.ny): self.X[i*self.ny + j,0] = self.xa + (i + 0.5)*self.hx self.X[i*self.ny + j,1] = self.ya + (j + 0.5)*self.hy self.u_acc = u_accuracy(self.X[:,0],self.X[:,