python 深度学习之tensorflow v2.3(二)

一 第一个神经网络实例

参照《TensorFlow自然语言处理-图姗 加格内达拉》 书籍学习,经整理形成文档。

1.1 神经网络步骤:

  1. 准备数据
  2. 定义 tensorflow 图
  3. 运行神经网络
    中间还有许多小步骤,会逐一介绍

1.2 代码实例:

import struct
import gzip
import os
from six.moves.urllib.request import urlretrieve
import numpy as np
# import matplotlib as plt
import matplotlib.pyplot as plt
import tensorflow as tf
tf.compat.v1.disable_eager_execution()


def maybe_download(url, filename, expected_bytes, force=False):
    """Download a file if not present, and make sure it's the right size."""
    if force or not os.path.exists(filename):
        print('Attempting to download:', filename)
        filename, _ = urlretrieve(url + filename, filename)
        print('\nDownload Complete!')
    statinfo = os.stat(filename)
    if statinfo.st_size == expected_bytes:
        print('Found and verified', filename)
    else:
        raise Exception(
            'Failed to verify ' + filename + '. Can you get to it with a browser?')
    return filename


def read_mnist(fname_img, fname_lbl):
    print('\nReading files %s and %s' % (fname_img, fname_lbl))

    with gzip.open(fname_img) as fimg:
        magic, num, rows, cols = struct.unpack(">IIII", fimg.read(16))
        print(num, rows, cols)
        img = (np.frombuffer(fimg.read(num * rows * cols), dtype=np.uint8).reshape(num, rows * cols)).astype(np.float32)
        print('(Images) Returned a tensor of shape ', img.shape)

        img = (img - np.mean(img)) / np.std(img)

    with gzip.open(fname_lbl) as flbl:
        # flbl.read(8) reads upto 8 bytes
        magic, num = struct.unpack(">II", flbl.read(8))
        lbl = np.frombuffer(flbl.read(num), dtype=np.int8)
        print('(Labels) Returned a tensor of shape: %s' % lbl.shape)
        print('Sample labels: ', lbl[:10])

    return img, lbl


# Download data if needed
url = 'http://yann.lecun.com/exdb/mnist/'
# training data
maybe_download(url, 'train-images-idx3-ubyte.gz', 9912422)
maybe_download(url, 'train-labels-idx1-ubyte.gz', 28881)
# testing data
maybe_download(url, 't10k-images-idx3-ubyte.gz', 1648877)
maybe_download(url, 't10k-labels-idx1-ubyte.gz', 4542)

# Read the training and testing data
train_inputs, train_labels = read_mnist('train-images-idx3-ubyte.gz', 'train-labels-idx1-ubyte.gz')
test_inputs, test_labels = read_mnist('t10k-images-idx3-ubyte.gz', 't10k-labels-idx1-ubyte.gz')


WEIGHTS_STRING = 'weights'
BIAS_STRING = 'bias'

batch_size = 100

img_width, img_height = 28,28
input_size = img_height * img_width
num_labels = 10

# resets the default graph Otherwise raises an error about already initialized variables
tf.compat.v1.reset_default_graph()

tf_inputs = tf.compat.v1.placeholder(shape=[batch_size, input_size], dtype=tf.float32, name = 'inputs')
tf_labels = tf.compat.v1.placeholder(shape=[batch_size, num_labels], dtype=tf.float32, name = 'labels')


# Defining the Tensorflow variables
def define_net_parameters():
    with tf.compat.v1.variable_scope('layer1'):
        tf.compat.v1.get_variable(WEIGHTS_STRING, shape=[input_size, 500],
                        initializer=tf.random_normal_initializer(0, 0.02))
        tf.compat.v1.get_variable(BIAS_STRING, shape=[500],
                        initializer=tf.random_uniform_initializer(0, 0.01))

    with tf.compat.v1.variable_scope('layer2'):
        tf.compat.v1.get_variable(WEIGHTS_STRING, shape=[500, 250],
                        initializer=tf.random_normal_initializer(0, 0.02))
        tf.compat.v1.get_variable(BIAS_STRING, shape=[250],
                        initializer=tf.
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
以下是在 Linux 系统上安装 TensorFlow Lite 2.3 的步骤: 1. 安装 TensorFlow 首先,你需要安装 TensorFlow。可以使用 pip 命令进行安装: ``` pip install tensorflow==2.3.0 ``` 2. 安装 Bazel TensorFlow Lite 2.3 需要使用 Bazel 进行编译,因此需要安装 Bazel。 官方推荐使用 Bazel 3.1.0 版本。可以使用以下命令进行安装: ``` sudo apt-get install curl gnupg curl -fsSL https://bazel.build/bazel-release.pub.gpg | gpg --dearmor > bazel.gpg sudo mv bazel.gpg /etc/apt/trusted.gpg.d/ echo "deb [arch=amd64] https://storage.googleapis.com/bazel-apt stable jdk1.8" | sudo tee /etc/apt/sources.list.d/bazel.list sudo apt-get update && sudo apt-get install bazel-3.1.0 ``` 3. 下载 TensorFlow Lite 源代码 可以从 TensorFlow 官网下载最新的 TensorFlow Lite 源代码,也可以使用 git 命令进行下载: ``` git clone https://github.com/tensorflow/tensorflow.git cd tensorflow git checkout v2.3.0 ``` 4. 编译 TensorFlow Lite 进入 TensorFlow Lite 源代码目录,执行以下命令进行编译: ``` bazel build -c opt //tensorflow/lite:libtensorflowlite.so ``` 编译完成后,可以在 bazel-bin/tensorflow/lite 目录下找到编译好的 libtensorflowlite.so 文件。 5. 安装 TensorFlow Lite Python 库 进入 TensorFlow Lite 源代码目录,执行以下命令安装 TensorFlow Lite Python 库: ``` pip install tensorflow/lite ``` 安装完成后,就可以使用 TensorFlow Lite Python 库了。 注意:在安装 TensorFlow Lite Python 库之前,需要先安装好 TensorFlow

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值