当前位置: 代码迷 >> 综合 >> Tensorflow1.x学习笔记
  详细解决方案

Tensorflow1.x学习笔记

热度:79   发布时间:2024-02-06 14:12:13.0

前言

机器学习和以往的编程的区别:

What we’ll do is we can get a bunch of examples from what we want to see, and then have the computer figure out the rules.

注意:我们所打上的标签(Labels)是Answers,而不是Rules。

Code & Explain

The ‘Hello Word’ in Neural Networks

model = keras.Sequential([keras.layers.Dense(units=1,input_shape=[1])])

Dense: define a layer of connected neurons

Sequencial:Succesive(连续不断的) layer


model.compile(optimizer='sgd',loss='mean_squared_error')

SGD:Stochastic Gradient Descent(随机梯度下降)

Mean Squared Error:均方误差

补充资料:

深度学习——优化器算法Optimizer详解(BGD、SGD、MBGD、Momentum、NAG、Adagrad、Adadelta、RMSprop、Adam)


xs = np.array([-1.0,0.0,1.0,2.0,3.0,4.0],dtype=float)
ys = np.array([-3.0,-1.0,1.0,3.0,5.0,7.0],dtype=float)

X和Y的值


model.fit(xs,ys,epochs=500)

代入创建好的模型中,迭代500次


print(model.predict([10.0]))

完成后,输入新的数据,并让模型预测结果


打印了500个,最后一个结果是

6/6 [==============================] - 0s 492us/step - loss: 5.0427e-05 [[18.979282]]


Why Not 19?

  • The first is that you trained it using a very little data. (数据少)

  • When using neural network, as they try to figure out the answer for everything, they deal in probability. (概率)

Fashion Mnist

Fashion-MNIST包含70k张图片,10种衣物,并且每张都是28x28灰度图像(数据量更小,但人眼依然可以识别)。

它是在Tensorflow这个API中的数据集。

mnist = keras.datasets.fashion_mnist

Declare an object of type MNIST (声明变量)


(training_images, training_labels), (test_images, test_labels) = mnist.load_data()

Loading it from the Keras database, and on this object, if we called load_data method, it will return 4 lists to us, the training data, the training labels, the testing data and the testing labels.

调用函数,返回四个列表,训练集,训练集标签,测试集,测试集标签。

Choose an ankle boot picture,its label is 09,Why are numbers?

  • Computer do better with numbers (计算机处理数字处理地更好)

  • Reduce bias 减少偏见。如果用英语标注,会偏向English Speakers. )

    (啊这,可是主流的编程语言不都是用英语写的嘛。但是换成数字确实免去了转换的麻烦)

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-u3fXfIWz-1596554400350)(C:\Users\86130\Desktop\Temp Gallary\TIM图片20200804190702.jpg)]


model = keras.Sequential([keras.layers.Flatten(input_shape=(28,28)),  keras.layers.Dense(128, activation=tf.nn.relu),keras.layers.Dense(10,activation=tf.nn.softmax) 
])

第一层是扁平层,每个图片的像素是28x28,把这个28x28的正方形变成一个简单的 linear array。

举个例子:把这个 (height, width, channel) 的三维数据压缩成长度为 (height × width × channel, 1) 的一维数组。

第二层是隐藏层,这一层有128个神经元。

#第三层是输出层,因为有10种衣物,所以得到的应该是10个概率,且和为1。


The complete code

import tensorflow as tf
import keras
# import matplotlib.pyplot as pltmnist = keras.datasets.fashion_mnist
(training_images, training_labels), (test_images, test_labels) = mnist.load_data()# 打印第0个样本的值
# plt.imshow(training_images[0])
# print(training_labels[0])
# print(training_images[0])training_images = training_images/255.0
test_images = test_images/255.0model = keras.Sequential([keras.layers.Flatten(input_shape=(28,28)),  #每个图片的像素是28x28keras.layers.Dense(128, activation=tf.nn.relu),keras.layers.Dense(10,activation=tf.nn.softmax) #因为有10种衣物,所以应该得到的是他们各自发概论
])model.compile(optimizer=tf.compat.v1.train.AdamOptimizer(),loss='sparse_categorical_crossentropy')model.fit(training_images,training_labels,epochs=5)model.evaluate(test_images,test_labels)
  相关解决方案