提交 9ea8d673 编写于 作者: John(°_°)…'s avatar John(°_°)…
name: Python application
on: [push, pull_request]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- name: Set up Python 3
uses: actions/setup-python@v1
with:
python-version: 3.x
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install flake8 pytest
pip install -r requirements.txt || true
- name: Lint with flake8
run: |
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Test with pytest
run: |
pytest || true
*.DS_Store
*.bak
\ No newline at end of file
......@@ -2,7 +2,15 @@
基于TensorFlow 2.0正式版!!!
包含电子书,配套源代码等,时间仓促,源代码还没有整理完全。
包含电子书,配套源代码等,时间仓促,源代码还没有整理完全~~
**Anyone interested in translating the Chinese version book into English is Welcome! pls contact me. We can co-author.**
- **本书被“机器之心”,“量子位”等权威媒体报导!**
- **本库在Github趋势日榜单连续多天全球排名第1!**
>主页上方有个“Clone or Download”绿色按钮,下载整个仓库即可。之所以显示在线打不开是因为Github在国外,连接不稳定就会提示打不开。
<p align="center">
<img src="assets/1.jpg" align="center" width="600">
......@@ -18,6 +26,12 @@ https://github.com/dragen1860/TensorFlow-2.x-Tutorials
- 联系邮箱(一般问题建议Github issues交流):liangqu.long AT gmail.com
- 使用本书本的任何内容时(仅限个人的非商业用途),请注明作者和Github链接
# 介绍短视频
https://www.bilibili.com/video/av75331861
# 目录
<p align="center">
......@@ -29,11 +43,13 @@ https://github.com/dragen1860/TensorFlow-2.x-Tutorials
# 初学者交流QQ群
人工智能101学院:295208768
- 人工智能101学院-免费2群:535205236
- 人工智能101学院-免费1群:295208768(已满)
# 配套视频课程
收费,提供答疑等全方位服务,不喜勿喷
收费,适合零基础、希望快速入门AI的朋友,提供答疑、指导等全方位服务
- 深度学习与TensorFlow入门实战
https://study.163.com/course/courseMain.htm?share=2&shareId=480000001847407&courseId=1209092816&_trace_c_p_k2_=9e74eb6f891d47cfaa6f00b5cb5f617c
......
import tensorflow as tf
assert tf.__version__.startswith('1.')
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
assert tf.__version__.startswith('2.')
# 1.创建计算图阶段
# 创建2个输入端子,指定类型和名字
......
import tensorflow as tf
from tensorflow.keras import datasets, layers, optimizers, Sequential, metrics
# 设置GPU使用方式
# 获取GPU列表
gpus = tf.config.experimental.list_physical_devices('GPU')
if gpus:
try:
# 设置GPU为增长式占用
for gpu in gpus:
tf.config.experimental.set_memory_growth(gpu, True)
except RuntimeError as e:
# 打印异常
print(e)
(xs, ys),_ = datasets.mnist.load_data()
print('datasets:', xs.shape, ys.shape, xs.min(), xs.max())
batch_size = 32
xs = tf.convert_to_tensor(xs, dtype=tf.float32) / 255.
db = tf.data.Dataset.from_tensor_slices((xs,ys))
db = db.batch(batch_size).repeat(30)
model = Sequential([layers.Dense(256, activation='relu'),
layers.Dense(128, activation='relu'),
layers.Dense(10)])
model.build(input_shape=(4, 28*28))
model.summary()
optimizer = optimizers.SGD(lr=0.01)
acc_meter = metrics.Accuracy()
for step, (x,y) in enumerate(db):
with tf.GradientTape() as tape:
# 打平操作,[b, 28, 28] => [b, 784]
x = tf.reshape(x, (-1, 28*28))
# Step1. 得到模型输出output [b, 784] => [b, 10]
out = model(x)
# [b] => [b, 10]
y_onehot = tf.one_hot(y, depth=10)
# 计算差的平方和,[b, 10]
loss = tf.square(out-y_onehot)
# 计算每个样本的平均误差,[b]
loss = tf.reduce_sum(loss) / x.shape[0]
acc_meter.update_state(tf.argmax(out, axis=1), y)
grads = tape.gradient(loss, model.trainable_variables)
optimizer.apply_gradients(zip(grads, model.trainable_variables))
if step % 200==0:
print(step, 'loss:', float(loss), 'acc:', acc_meter.result().numpy())
acc_meter.reset_states()
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
从MNIST中读取原始图片并保存、读取标签数据并保存。
MNIST文件结构分析可以参考:https://blog.csdn.net/justidle/article/details/103149253
"""
"""
使用方法:
1、将MNIST的文件下载到本地。
2、在py文件所在目录下,建立mnist_data目录。然后将MNIST的四个文件拷贝到mnist_data目录,并解压
3、在py文件所在目录下,建立test目录,改目录用于存放解压出的图片文件和标签文件
"""
import struct
import numpy as np
import PIL.Image
def read_image(filename):
#打开文件
f = open(filename, 'rb')
#读取文件内容
index = 0
buf = f.read()
#关闭文件
f.close()
#解析文件内容
#>IIII 表示使用大端规则,读取四个整型
magic, numImages, rows, columns = struct.unpack_from('>IIII', buf, index)
index += struct.calcsize('>IIII')
for i in range(0, numImages):
# L代表灰度图片
image = PIL.Image.new('L', (columns, rows))
for x in range(rows):
for y in range(columns):
# ‘>B' 读取一个字节
image.putpixel((y,x), int(struct.unpack_from('>B', buf, index)[0]))
index += struct.calcsize('>B')
print('save ' + str(i) + 'image')
image.save('mnist_data/test/'+str(i)+'.png')
def read_label(filename, saveFilename):
f = open(filename, 'rb')
index = 0
buf = f.read()
f.close()
magic, labels = struct.unpack_from('>II' , buf , index)
index += struct.calcsize('>II')
labelArr = [0] * labels
for x in range(labels):
labelArr[x] = int(struct.unpack_from('>B', buf, index)[0])
index += struct.calcsize('>B')
save = open(saveFilename, 'w')
save.write(','.join(map(lambda x: str(x), labelArr)))
save.write('\n')
save.close()
print('save labels success')
if __name__ == '__main__':
#注意t10k-images-idx3-ubyte里面一共有10,000张图片
read_image('mnist_data/t10k-images-idx3-ubyte')
read_label('mnist_data/t10k-labels-idx1-ubyte', 'mnist_data/test/label.txt')
\ No newline at end of file
......@@ -16,7 +16,7 @@ out
#%%
net.embeddings
net.embeddings.trainable
net.trainable = Flase
net.trainable = False
#%%
# 从预训练模型中加载词向量表
embed_glove = load_embed('glove.6B.50d.txt')
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册