神经网络压缩(3):Learning both Weights and Connections for Efficient Neural Network

Learning both Weights and Connections for Efficient Neural Networks

Abstract

  1. 目的:减少存储量和计算量,使网络更适合在移动设备上运行
  2. 方法:简单地说是只学习重要的连接,修剪冗余的连接减少模型参数
  3. 步骤:train,学习哪些连接是重要的
       prune,剪去不重要的连接
       retrain,finetune剩下的连接的参数
       prune和retrain可以反复迭代进行
  4. 结果:AlexNet减少模型参数量61million->6.7million,没有精度损失
       VGG-16减少模型参数量138million->10.3million,没有精度损失
  1. 8-bit integer (vs 32-bit floating point) activations,用8位int型的activation代替32位float
  2. 量化(Deep Compress论文中介绍的)和剪枝可以同时使用,以获得进一步收益
  3. Network in Network和GoogleNet模型中使用了global average pooling代替FC层来减少参数,但在使用ImageNet的参数时,需要另外增加一个线性层。(这是在网络结构方面寻求参数缩减)
  4. HashNet,和prune结合可能会得到更好的效果

  • 9
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现 Monte Carlo fPINNs 的代码需要一些准备工作。首先需要安装 TensorFlow、NumPy、SciPy 和 Matplotlib 库。其次需要准备数据,包括输入数据、输出数据以及模型参数。 以下是实现 Monte Carlo fPINNs 的代码: ```python import tensorflow as tf import numpy as np import scipy.io import matplotlib.pyplot as plt # Load data data = scipy.io.loadmat('data.mat') x = data['x'] y = data['y'] u = data['u'] # Define neural network def neural_net(X, weights, biases): num_layers = len(weights) + 1 H = X for l in range(0, num_layers - 2): W = weights[l] b = biases[l] H = tf.sin(tf.add(tf.matmul(H, W), b)) W = weights[-1] b = biases[-1] Y = tf.add(tf.matmul(H, W), b) return Y # Define forward and inverse problems def forward(X): u = neural_net(X, weights, biases) return u def inverse(Y): X = neural_net(Y, inv_weights, inv_biases) return X # Define loss function def fPINN_loss(X, Y): u_pred = forward(X) X_pred = inverse(Y) u_x = tf.gradients(u_pred, X)[0] u_xx = tf.gradients(u_x, X)[0] u_y = tf.gradients(u_pred, Y)[0] f = u_xx + u_y f_pred = forward(X_pred) loss = tf.reduce_mean(tf.square(u - u_pred)) + tf.reduce_mean(tf.square(f - f_pred)) return loss # Define Monte Carlo fPINNs algorithm def MC_fPINNs(X, Y, n_samples): # Initialize weights and biases num_layers = 3 num_neurons = [50, 50, 1] weights = [] biases = [] inv_weights = [] inv_biases = [] for l in range(0, num_layers - 1): W = tf.Variable(tf.random_normal([num_neurons[l], num_neurons[l+1]]), dtype=tf.float32) b = tf.Variable(tf.zeros([1, num_neurons[l+1]]), dtype=tf.float32) weights.append(W) biases.append(b) for l in range(0, num_layers - 1): W = tf.Variable(tf.random_normal([num_neurons[l], num_neurons[l+1]]), dtype=tf.float32) b = tf.Variable(tf.zeros([1, num_neurons[l+1]]), dtype=tf.float32) inv_weights.append(W) inv_biases.append(b) # Define optimizer optimizer = tf.train.AdamOptimizer() # Define training operation train_op = optimizer.minimize(fPINN_loss(X, Y)) # Define session sess = tf.Session() # Initialize variables init = tf.global_variables_initializer() sess.run(init) # Train model for i in range(n_samples): sess.run(train_op) if i % 100 == 0: loss = sess.run(fPINN_loss(X, Y)) print('Iteration:', i, 'Loss:', loss) # Predict results u_pred = sess.run(forward(X)) X_pred = sess.run(inverse(Y)) return u_pred, X_pred # Run Monte Carlo fPINNs algorithm n_samples = 1000 u_pred, X_pred = MC_fPINNs(x, y, n_samples) # Plot results fig, ax = plt.subplots() ax.plot(x, u, 'b-', label='Exact') ax.plot(x, u_pred, 'r--', label='Predicted') ax.set_xlabel('x') ax.set_ylabel('u') ax.legend() plt.show() ``` 在此代码中,我们首先加载数据(输入数据 x、输出数据 y 和真实值 u),然后定义神经网络模型,包括正向问题和反向问题。接下来,我们定义损失函数 fPINN_loss 和 Monte Carlo fPINNs 算法。在算法中,我们使用随机采样的方式进行训练,并且在每个迭代步骤中输出损失值。最后,我们运行算法并绘制预测结果。 需要注意的是,Monte Carlo fPINNs 算法的实现需要一定的计算资源,因此在实际应用中需要考虑计算效率和可扩展性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值