python神经网络

用Keras搭建神经网络 简单模版(六)——Autoencoder 自编码

落花浮王杯 提交于 2019-11-27 00:51:52
import numpy as np np.random.seed(1337) from keras.datasets import mnist from keras.models import Model from keras.layers import Dense, Input import matplotlib.pyplot as plt (x_train,y_train),(x_test,y_test) = mnist.load_data() x_train = x_train.astype('float32') / 255.-0.5 #(-0.5,0.5)的区间 x_test = x_test.astype('float32') / 255.-0.5 x_train = x_train.reshape((x_train.shape[0],-1)) x_test = x_test.reshape((x_test.shape[0],-1)) print(x_train.shape) print(x_test.shape) # 最终压缩成2个 encoding_dim = 2 # 输入 input_img = Input(shape=(784,)) # encoder layers encoded = Dense(128, activation='relu')(input

使用PyTorch创建神经网络

China☆狼群 提交于 2019-11-26 19:24:11
2019年年初,ApacheCN组织志愿者翻译了PyTorch1.0版本中文文档( github地址 ),同时也获得了PyTorch官方授权,我相信已经有许多人在 中文文档官网 上看到了。不过目前 校对 还缺人手,希望大家踊跃参与。之前一段时间我们和PyTorch的有关负责人Bruce Lin一直在进行邮件交流。在之后适当的时候,我们会组织志愿者进行其他有关PyTorch的项目,欢迎大家加入我们,关注我们。更希望我们的一系列工作能够对大家有所帮助。 译者: bat67 校对者: FontTian 可以使用 torch.nn 包来构建神经网络. 我们已经介绍了 autograd , nn 包则依赖于 autograd 包来定义模型并对它们求导。一个 nn.Module 包含各个层和一个 forward(input) 方法,该方法返回 output 。 例如,下面这个神经网络可以对数字进行分类: 这是一个简单的前馈神经网络(feed-forward network)。它接受一个输入,然后将它送入下一层,一层接一层的传递,最后给出输出。 一个神经网络的典型训练过程如下: 定义包含一些可学习参数(或者叫权重)的神经网络 在输入数据集上迭代 通过网络处理输入 计算损失(输出和正确答案的距离) 将梯度反向传播给网络的参数 更新网络的权重,一般使用一个简单的规则: weight = weight

Python绘制神经网络精度与损失曲线

时光怂恿深爱的人放手 提交于 2019-11-26 10:40:47
1 """ python绘制神经网络训练精度与损失 """ 2 3 # 导入相应包 4 from matplotlib import pyplot as plt 5 6 # 构造绘图函数 7 def drow(history): 8 epochs = range(1, len(history['loss']) + 1) 9 plt.plot(epochs, history['loss'], 'bo', label='Training loss') 10 plt.plot(epochs, history['val_loss'], 'b', label='Validation loss') 11 plt.title('Training and Validation loss') 12 plt.xlabel('Epochs') 13 plt.ylabel('Loss') 14 plt.legend() 15 16 plt.imsave('E:/acc_and_loss/Training and Validation loss.jpg') 17 18 plt.figure() 19 epochs = range(1, len(history['acc']) + 1) 20 plt.plot(epochs, history['acc'], 'bo', label='Training acc'