神经网络入门

神经网络入门

1.基础知识代码

import pickle as pk
# python2有cPickle,但是在python3下,是没有cPickle的 python 为import cPickle
import os
import numpy as np
import matplotlib.pyplot as plt

CIFAR_DIR = "D:\\xunlian"
print(os.listdir(CIFAR_DIR))
with open(os.path.join(CIFAR_DIR, "data_batch_1"), 'rb') as f:
    data = pk.load(f, encoding='bytes')
    # tensorflow 1.x 为 data = cPickle.load(f)
    # pickle.load()默认解码是以encoding=”ASCII”解码的,而我们要载入的文件并不是以”ASCII”形式存储的,所以要改变参数encoding=” ”
    # print(type(data))
    # print(data.keys())
    image_arr = data[b'data'][100]
    # python 2.x为image_arr = data['data'][100]
    image_arr = image_arr.reshape((3, 32, 32))
    image_arr = image_arr.transpose((1, 2, 0))
    plt.imshow(image_arr)
    plt.show()
    # python 2.x直接plt.imshow()

  1)因为tensorflow2.x(python3.x)和tensorflow1.x(python2.x)很多东西不兼容,所以关键部分写了注释,大家可以更加自己安装的版本选择代码
  2)CIFAR_DIR变量为文件路径,这里路径一定要是\\,本代码用的是cifar10-batch数据集

2.二分类逻辑斯蒂回归模型实现

import tensorflow as tf
tf.compat.v1.disable_eager_execution() #关闭紧急执行。
import pickle as pk
import os
import numpy as np
CIFAR_DIR = "D:\\xunlian"

def load_data(filename):
    with open(filename, 'rb') as f:
        data = pk.load(f, encoding='bytes')
        return data[b'data'], data[b'labels']

class CifarData:
    def __init__(self, filenames, need_shuffle):
        all_data = []
        all_labels = []
        for filename in filenames:
            data, labels =load_data(filenames)
            for item, label in zip(data, labels):
                if label in [0,1]:
                    all_data.append(item)
                    all_labels.append(label)
        self._data = np.vstack(all_data)
        self._data = self._data / 127.5 -1
        self._labels = np.hstack(all_labels)
        self._num_examples = self._data.shape[0]
        self._need_shuffle = need_shuffle
        self._indicator = 0
        if self._need_shuffle:
            self._shuffle_data()
    def _shuffle_data(self):
        p = np.random.permutation(self._num_examples)
        self._data = self._data[p]
        self._labels = self._labels[p]
    def next_batch(self, batch_size):
        end_indicator = self._indicator + batch_size
        if end_indicator > self._num_examples:
            if self._need_shuffle:
                self._shuffle_data()
                self._indicator = 0
                end_indicator = batch_size
            else:
                raise Exception("have no more examples")
        if end_indicator > self._num_examples:
            raise Exception("batch size is larger than all examples")
        batch_data = self._data[self._indicator:end_indicator]
        batch_labels = self._labels[self._indicator:end_indicator]
        self._indicator = end_indicator
        return batch_data, batch_labels
# train_filenames = [os.path.join(CIFAR_DIR, 'data_batch_%d' % i) for i in range(1 , 6)]
# test_filenames = [os.path.join(CIFAR_DIR, 'test_batch')]
# train_data = CifarData(train_filenames, True)
# test_data = CifarData(test_filenames, False)
test_filenames = os.path.join(CIFAR_DIR, 'test_batch')
test_data = CifarData(test_filenames, False)
for i in range(1 , 6):
    train_filenames = os.path.join(CIFAR_DIR, 'data_batch_%d' % i)
train_data = CifarData(train_filenames, True)

x = tf.compat.v1.placeholder(tf.float32, [None,3072])
y = tf.compat.v1.placeholder(tf.int64, [None])
y_reshaped = tf.reshape(y, (-1,1))
y_reshaped_float = tf.cast(y_reshaped, tf.float32)
w = tf.compat.v1.get_variable('w', [x.get_shape()[-1], 1], initializer=tf.random_normal_initializer(0, 1))
b = tf.compat.v1.get_variable('b', [1], initializer=tf.constant_initializer(0.0))
y_ = tf.matmul(x, w) + b
p_y_1 = tf.nn.sigmoid(y_)
loss = tf.reduce_mean(tf.square(y_reshaped_float - p_y_1))
predict = p_y_1 > 0.5
correct_prediction = tf.equal(tf.cast(predict, tf.int64), y_reshaped)
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float64))

with tf.name_scope('train_op'):
    train_op = tf.compat.v1.train.AdamOptimizer(1e-3).minimize(loss)

init = tf.compat.v1.global_variables_initializer()
batch_size = 20
train_steps = 100000
test_steps = 100
with tf.compat.v1.Session() as sess:
    sess.run(init)
    for i in range(train_steps):
        batch_data, batch_labels = train_data.next_batch(batch_size)
        loss_val, acc_val, _ = sess.run([loss, accuracy, train_op],feed_dict = {x: batch_data, y: batch_labels})
        if i % 500 == 0:
            print('[Train] Step: %d, loss: %4.5f, acc: %4.5f' % (i, loss_val, acc_val))
        if i % 5000 == 0:
            test_data = CifarData(test_filenames, False)
            all_test_acc_val = []
            for j in range(test_steps):
                test_batch_data, test_batch_labels = test_data.next_batch(batch_size)
                test_acc_val = sess.run([accuracy], feed_dict = {x:test_batch_data, y:test_batch_labels})
                all_test_acc_val.append(test_acc_val)
            test_acc = np.mean(all_test_acc_val)
            print('[Test] Step: %d, acc: %4.5f' % (i, test_acc))
这个代码是单个神经元的代码

3.多分类逻辑斯蒂回归模型实现

import tensorflow as tf
tf.compat.v1.disable_eager_execution() #关闭紧急执行。
import pickle as pk
import os
import numpy as np
CIFAR_DIR = "D:\\xunlian"

def load_data(filename):
    with open(filename, 'rb') as f:
        data = pk.load(f, encoding='bytes')
        return data[b'data'], data[b'labels']

class CifarData:
    def __init__(self, filenames, need_shuffle):
        all_data = []
        all_labels = []
        for filename in filenames:
            data, labels =load_data(filenames)
            all_data.append(data)
            all_labels.append(labels)
        self._data = np.vstack(all_data)
        self._data = self._data / 127.5 -1
        self._labels = np.hstack(all_labels)
        self._num_examples = self._data.shape[0]
        self._need_shuffle = need_shuffle
        self._indicator = 0
        if self._need_shuffle:
            self._shuffle_data()
    def _shuffle_data(self):
        p = np.random.permutation(self._num_examples)
        self._data = self._data[p]
        self._labels = self._labels[p]
    def next_batch(self, batch_size):
        end_indicator = self._indicator + batch_size
        if end_indicator > self._num_examples:
            if self._need_shuffle:
                self._shuffle_data()
                self._indicator = 0
                end_indicator = batch_size
            else:
                raise Exception("have no more examples")
        if end_indicator > self._num_examples:
            raise Exception("batch size is larger than all examples")
        batch_data = self._data[self._indicator:end_indicator]
        batch_labels = self._labels[self._indicator:end_indicator]
        self._indicator = end_indicator
        return batch_data, batch_labels

test_filenames = os.path.join(CIFAR_DIR, 'test_batch')
test_data = CifarData(test_filenames, False)
for i in range(1 , 6):
    train_filenames = os.path.join(CIFAR_DIR, 'data_batch_%d' % i)
train_data = CifarData(train_filenames, True)

x = tf.compat.v1.placeholder(tf.float32, [None,3072])
y = tf.compat.v1.placeholder(tf.int64, [None])
w = tf.compat.v1.get_variable('w', [x.get_shape()[-1], 10], initializer=tf.random_normal_initializer(0, 1))
b = tf.compat.v1.get_variable('b', [10], initializer=tf.constant_initializer(0.0))
y_ = tf.matmul(x, w) + b
# loss = tf.losses.sparse_softmax_cross_entropy(labels=y, logits=y_)
p_y = tf.nn.softmax(y_)
y_one_hot = tf.one_hot(y, 10, dtype=tf.float32)
loss = tf.reduce_mean(tf.square(y_one_hot - p_y))
predict = tf.argmax(y_,1)
correct_prediction = tf.equal(predict,y)
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float64))

with tf.name_scope('train_op'):
    train_op = tf.compat.v1.train.AdamOptimizer(1e-3).minimize(loss)

init = tf.compat.v1.global_variables_initializer()
batch_size = 20
train_steps = 50000
test_steps = 100
with tf.compat.v1.Session() as sess:
    sess.run(init)
    for i in range(train_steps):
        batch_data, batch_labels = train_data.next_batch(batch_size)
        loss_val, acc_val, _ = sess.run([loss, accuracy, train_op],feed_dict = {x: batch_data, y: batch_labels})
        if i % 500 == 0:
            print('[Train] Step: %d, loss: %4.5f, acc: %4.5f' % (i, loss_val, acc_val))
        if i % 5000 == 0:
            test_data = CifarData(test_filenames, False)
            all_test_acc_val = []
            for j in range(test_steps):
                test_batch_data, test_batch_labels = test_data.next_batch(batch_size)
                test_acc_val = sess.run([accuracy], feed_dict = {x:test_batch_data, y:test_batch_labels})
                all_test_acc_val.append(test_acc_val)
            test_acc = np.mean(all_test_acc_val)
            print('[Test] Step: %d, acc: %4.5f' % (i, test_acc))
这个单个是神经网络的代码
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值