典型神经网络模型:(图片来源:https://github.com/madalinabuzau/tensorflow-eager-tutorials) 保持更新,更多内容请关注 cnblogs.com/xuyaowen;原文:https://www.cnblogs.com/xuyaowen/p/tensorflow-nn-Eager.html
# 创建神经网络, 使用tensorboard 展示graph/histogram/scalarimport tensorflow as tf import numpy as np import matplotlib.pyplot as plt # 若没有 pip install matplotlib# 定义一个神经层def add_layer(inputs, in_size, out_size,n_layer, activation_function=None):#add one more layer and return the output of this layerlayer_name="layer%s"%n_layerwith tf.name_scope(layer_name):with tf.name_scope(‘Weights‘...
使用captcha.image.Image 生成随机验证码,随机生成的验证码为0到9的数字,验证码有4位数字组成,这是一个自己生成验证码,自己不断训练的模型使用三层卷积层,三层池化层,二层全连接层来进行组合第一步:定义生成随机验证码图片number = [‘0‘,‘1‘,‘2‘,‘3‘,‘4‘,‘5‘,‘6‘,‘7‘,‘8‘,‘9‘] # alphabet = [‘a‘,‘b‘,‘c‘,‘d‘,‘e‘,‘f‘,‘g‘,‘h‘,‘i‘,‘j‘,‘k‘,‘l‘,‘m‘,‘n‘,‘o‘,‘p‘,‘q‘,‘...
import tensorflow as tf# 输入数据from tensorflow.examples.tutorials.mnist import input_datamnist = input_data.read_data_sets("E:\\MNIST_data", one_hot=True)# 定义网络的超参数 learning_rate = 0.001 training_iters = 200000 batch_size = 128 display_step = 5# 定义网络的参数 # 输入的维度 (img shape: 28*28) n_input = 784 # 标记的维度 (0-9 digits) n_classes = 10 # Dropout的概率,输出的可能性 dropout = ...
使用人工神经网络完成手写数字识别任务。具体要求如下: (1)batch_size和step_num自定义,把loss值打印出来。 (2)神经网络的层数、节点数目、激活函数自定义。(记录心得) (3)使用tensorboard把计算图显示出来。一、初始数据如下:batch_size=64lr = 0.01 #学习率step_num = 6000 #计算6000次计算的神经元层数是3层使用的是随机梯度下降算法 tf.train.GradientDescentOptimizer()计算出来的准确度为:0.717 二、参数调优过程...
加载cifar10图片集并准备将图片进行灰度化 from keras.datasets import cifar10def rgb2gray(rgb):#把彩色图转化为灰度图,如果当前像素点为[r,g,b],那么对应的灰度点为0.299*r+0.587*g+0.114*breturn np.dot(rgb[...,:3], [0.299, 0.587, 0.114])(x_train, _),(x_test, _) = cifar10.load_data()img_rows = x_train.shape[1] img_cols = x_train.shape[2] channels = x_train.shape[3]#将100张彩色原图集合在一起显示 imgs = x_t...
from keras.layers import SimpleRNN model = Sequential() model.add(embedding_layer) model.add(SimpleRNN(32)) #当结果是输出多个分类的概率时,用softmax激活函数,它将为30个分类提供不同的可能性概率值 model.add(layers.Dense(len(int_category), activation=softmax))#对于输出多个分类结果,最好的损失函数是categorical_crossentropy model.compile(optimizer=rmsprop, loss=categorical_crossentropy, metrics=[accurac...
from keras.layers import LSTM model = Sequential() model.add(embedding_layer) model.add(LSTM(32)) #当结果是输出多个分类的概率时,用softmax激活函数,它将为30个分类提供不同的可能性概率值 model.add(layers.Dense(len(int_category), activation=softmax))#对于输出多个分类结果,最好的损失函数是categorical_crossentropy model.compile(optimizer=rmsprop, loss=categorical_crossentropy, metrics=[accuracy]) histor...
from keras.layers import model = Sequential() model.add(embedding_layer) #使用一维卷积网络切割输入数据,参数5表示每各个单词作为切割小段 model.add(layers.Conv1D(32, 5, activation=relu)) #参数3表示,上层传下来的数据中,从每3个数值中抽取最大值 model.add(layers.MaxPooling1D(3)) #添加一个有记忆性的GRU层,其原理与LSTM相同,运行速度更快,准确率有所降低 model.add(layers.GRU(32, dropout=0.1))model.add(lay...
from keras.layers import Dense, Input from keras.layers import Conv2D, Flatten from keras.layers import Reshape, Conv2DTranspose from keras.models import Model from keras.datasets import mnist from keras import backend as Kimport numpy as np import matplotlib.pyplot as plt#加载手写数字图片数据 (x_train, _), (x_test, _) = mnist.load_data() image_size = x_train.shape[1]#把图片大小统一转换成28*28,并把...
#为图像像素点增加高斯噪音 noise = np.random.normal(loc=0.5, scale = 0.5, size = x_train.shape) x_train_noisy = x_train + noise noise = np.random.normal(loc=0.5, scale = 0.5, size = x_test.shape) x_test_noisy = x_test + noise #添加噪音值后,像素点值可能会超过1或小于0,我们把这些值调整到[0,1]之间 x_train_noisy = np.clip(x_train_noisy, 0., 1.) x_test_noisy = np.clip(x_test_noisy, 0., 1.)autoencoder =...
import numpy as np samples = [The cat jump over the dog, The dog ate my homework]#我们先将每个单词放置到一个哈希表中 token_index = {} for sample in samples:#将一个句子分解成多个单词for word in sample.split():if word not in token_index:token_index[word] = len(token_index) + 1#设置句子的最大长度 max_length = 10 results = np.zeros((len(samples), max_length, max(token_index.values()) + 1)) for i, samp...
import matplotlib.pyplot as plt import numpy as np import matplotlib.mlab as mlab import mathmu = 0 variance = 1 sigma = math.sqrt(variance) x = np.linspace(mu - 3*sigma, mu + 3*sigma, 100) plt.plot(x, mlab.normpdf(x, mu, sigma)) plt.show()import scipy, scipy.stats x = scipy.linspace(0,10,11) pmf = scipy.stats.binom.pmf(x,10,0.5) import pylab pylab.plot(x,pmf)
def conv_(img, conv_filter):filter_size = conv_filter.shape[1]result = numpy.zeros((img.shape))print(loop r: , numpy.uint16(numpy.arange(filter_size/2.0,img.shape[0]-filter_size/2.0+1)))#Looping through the image to apply the convolution operation.for r in numpy.uint16(numpy.arange(filter_size/2.0,img.shape[0]-filter_size/2.0+1)):for c in numpy.uint16(numpy.arange(filter_size/2.0,img.shape[1]-filt...
import pandas as pd data_path = /Users/chenyi/Documents/housing.csv housing = pd.read_csv(data_path) housing.info()housing.head()housing.describe()housing.hist(bins=50, figsize=(15,15)) housing[ocean_proximity].value_counts()import seaborn as sns total_count = housing[ocean_proximity].value_counts() plt.figure(figsize=(10,5)) sns.barplot(total_count.index, total_count.values, alpha=0.7) pl...