1.神经网络与深度学习
在神经网络的世界中,如果隐藏层为一层,就是单层神经网络,这是最简单的神经网络;如果有多层,则为多层感知机,深度学习是多层感知机的一种。深度神经网络是一种全连接的多层感知机,包括DNN、CNN和RNN。
2.TensorFlow
用于基于数据流图的计算,可以将复杂的数据结构传输至人工智能神经网中进行分析和处理。基本的组件有以下四个:
- 操作:算法可以看成是操作的叠加,一个操作可以有不定个输入,产生不定个输出。
- 张量:计算图中的每个边表示数据从一个操作流到另一个操作,这些数据被表示为张量,一个张量可以看成多维的数组或者高维的矩阵。在TensorFlow中,run之前的张量并未分配空间,仅仅表示了一中数据的抽象,用来连接不同的节点,表示数据在不同操作之间的流动。
- 变量
- 会话:负责分配和管理各种资源
3.在TensorFlow下识别验证码
使用TensorFlow的softmax回归算法,使用one-hot编码处理标记数据,使用N为状态寄存器来对N个状态进行编码,每个状态都有其独立的寄存器位,并且在任一时刻只有其中一位有效。
import tensorflow as tf
import pickle
import gzip
def get_one_hot(x,size=10):
v=[]
for x1 in x:
x2=[0]*size
x2[(x1-1)]=1
v.append(x2)
return v
def load_data():
with gzip.open('/Users/zhanglipeng/Data/MNIST/mnist.pkl.gz') as fp:
training_data, valid_data, test_data = pickle.load(fp)
return training_data, valid_data, test_data
training_data, valid_data, test_dat=load_data()
x_training_data,y_training_data=training_data
x1,y1=test_dat
y_training_data=get_one_hot(y_training_data)
y1=get_one_hot(y1)
batch_size=100
x = tf.placeholder("float", [None, 784])
W = tf.Variable(tf.zeros([784,10]))
b = tf.Variable(tf.zeros([10]))
y = tf.nn.softmax(tf.matmul(x,W) + b)
y_ = tf.placeholder("float", [None,10])
cross_entropy = -tf.reduce_sum(y_*tf.log(y))
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)
init = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init)
for i in range(int(len(x_training_data)/batch_size)):
batch_xs=x_training_data[(i*batch_size):((i+1)*batch_size)]
batch_ys=y_training_data[(i*batch_size):((i+1)*batch_size)]
sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print(sess.run(accuracy, feed_dict={x: x1, y_: y1}))
使用多层感知机:
import tensorflow as tf
import pickle
import gzip
def get_one_hot(x,size=10):
v=[]
for x1 in x:
x2=[0]*size
x2[(x1-1)]=1
v.append(x2)
return v
def load_data():
with gzip.open('/Users/zhanglipeng/Data/MNIST/mnist.pkl.gz') as fp:
training_data, valid_data, test_data = pickle.load(fp)
return training_data, valid_data, test_data
training_data, valid_data, test_dat=load_data()
x_training_data,y_training_data=training_data
x1,y1=test_dat
y_training_data=get_one_hot(y_training_data)
y1=get_one_hot(y1)
batch_size=100
in_units=784
h1_units=300
W1=tf.Variable(tf.truncated_normal([in_units,h1_units],stddev=0.1))
b1=tf.Variable(tf.zeros([h1_units]))
W2=tf.Variable(tf.zeros([h1_units,10]))
b2=tf.Variable(tf.zeros([10]))
x = tf.placeholder(tf.float32, [None, in_units])
keep_prob=tf.placeholder(tf.float32)
hidden1=tf.nn.relu(tf.matmul(x,W1)+b1)
hidden1_drop=tf.nn.dropout(hidden1,keep_prob)
y = tf.nn.softmax(tf.matmul(hidden1_drop,W2) + b2)
y_ = tf.placeholder(tf.float32, [None,10])
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_*tf.log(y),reduction_indices=[1]))
train_step = tf.train.AdagradOptimizer(0.3).minimize(cross_entropy)
init = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init)
for i in range(int(len(x_training_data)/batch_size)):
batch_xs=x_training_data[(i*batch_size):((i+1)*batch_size)]
batch_ys=y_training_data[(i*batch_size):((i+1)*batch_size)]
sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys,keep_prob:0.75})
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print(sess.run(accuracy, feed_dict={x: x1, y_: y1,keep_prob:1.0}))
WARNING:tensorflow:From /anaconda2/envs/python27/lib/python2.7/site-packages/tensorflow/python/util/tf_should_use.py:189: initialize_all_variables (from tensorflow.python.ops.variables) is deprecated and will be removed after 2017-03-02.
Instructions for updating:
Use `tf.global_variables_initializer` instead.
2019-02-21 23:39:41.164438: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX AVX2 FMA
2019-02-21 23:39:41.165351: I tensorflow/core/common_runtime/process_util.cc:69] Creating new thread pool with default inter op setting: 12. Tune using inter_op_parallelism_threads for best performance.
0.9548
4.在TensorFlow下使用DNN算法识别垃圾邮件
import tensorflow as tf
from tensorflow.contrib.learn.python import learn
from sklearn import metrics
from sklearn.model_selection import train_test_split
import numpy as np
from sklearn.naive_bayes import GaussianNB
import os
from sklearn.feature_extraction.text import CountVectorizer
from tensorflow.contrib.layers.python.layers import encoders
from sklearn import svm
MAX_DOCUMENT_LENGTH = 50
EMBEDDING_SIZE = 50
n_words=0
def load_one_file(filename):
x=""
with open(filename) as f:
for line in f:
#line=line.strip('\n')
x+=line
return x
def load_files(rootdir,label):
list = os.listdir(rootdir)
x=[]
y=[]
for i in range(0, len(list)):
path = os.path.join(rootdir, list[i])
if os.path.isfile(path):
print "Load file %s" % path
y.append(label)
x.append(load_one_file(path))
return x,y
def load_data():
x=[]
y=[]
x1,y1=load_files("/Users/zhanglipeng/Data/movie-review-data/review_polarity/txt_sentoken/pos/",0)
x2,y2=load_files("/Users/zhanglipeng/Data/movie-review-data/review_polarity/txt_sentoken/neg/", 1)
x=x1+x2
y=y1+y2
return x,y
def rnn_model(features, target):
"""RNN model to predict from sequence of words to a class."""
# Convert indexes of words into embeddings.
# This creates embeddings matrix of [n_words, EMBEDDING_SIZE] and then
# maps word indexes of the sequence into [batch_size, sequence_length,
# EMBEDDING_SIZE].
word_vectors = tf.contrib.layers.embed_sequence(
features, vocab_size=n_words, embed_dim=EMBEDDING_SIZE, scope='words')
# Split into list of embedding per word, while removing doc length dim.
# word_list results to be a list of tensors [batch_size, EMBEDDING_SIZE].
word_list = tf.unstack(word_vectors, axis=1)
# Create a Gated Recurrent Unit cell with hidden size of EMBEDDING_SIZE.
cell = tf.contrib.rnn.GRUCell(EMBEDDING_SIZE)
# Create an unrolled Recurrent Neural Networks to length of
# MAX_DOCUMENT_LENGTH and passes word_list as inputs for each unit.
_, encoding = tf.contrib.rnn.static_rnn(cell, word_list, dtype=tf.float32)
# Given encoding of RNN, take encoding of last step (e.g hidden size of the
# neural network of last step) and pass it as features for logistic
# regression over output classes.
target = tf.one_hot(target, 15, 1, 0)
logits = tf.contrib.layers.fully_connected(encoding, 15, activation_fn=None)
loss = tf.contrib.losses.softmax_cross_entropy(logits, target)
# Create a training op.
train_op = tf.contrib.layers.optimize_loss(
loss,
tf.contrib.framework.get_global_step(),
optimizer='Adam',
learning_rate=0.01)
return ({
'class': tf.argmax(logits, 1),
'prob': tf.nn.softmax(logits)
}, loss, train_op)
def main(unused_argv):
x,y=load_data()
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.4, random_state=0)
vp = learn.preprocessing.VocabularyProcessor(max_document_length=MAX_DOCUMENT_LENGTH, min_frequency=1)
x_train = np.array(list(vp.fit_transform(x_train)))
x_test = np.array(list(vp.transform(x_test)))
n_words=len(vp.vocabulary_)
print('Total words: %d' % n_words)
gnb = GaussianNB()
y_predict = gnb.fit(x_train, y_train).predict(x_test)
score = metrics.accuracy_score(y_test, y_predict)
print('NB Accuracy: {0:f}'.format(score))
feature_columns = tf.contrib.learn.infer_real_valued_columns_from_input(x_train)
classifier = tf.contrib.learn.DNNClassifier(
feature_columns=feature_columns, hidden_units=[500,10], n_classes=2)
classifier.fit(x_train, y_train, steps=5000, batch_size=10)
y_predict=list(classifier.predict(x_test, as_iterable=True))
score = metrics.accuracy_score(y_test, y_predict)
print('DNN Accuracy: {0:f}'.format(score))
if __name__ == '__main__':
tf.app.run()