TensorFlow neural network model, create a complete MLP neural network model

create training set

#!/usr/bin/env python
# -*- coding=utf-8 -*-

import tensorflow as tf
import numpy as np

#创建数据集
x1=np.random.random((500,1))
x2=np.random.random((500,1))+1
x_train=np.concatenate((x1, x2))

y1=np.zeros((500,), dtype=int)
y2=np.ones((500,), dtype=int)
y_train=np.concatenate((y1, y2))

build model

#创建神经网络模型
model = tf.keras.models.Sequential([
  tf.keras.layers.Dense(10, activation=tf.nn.relu, input_dim=1),
  #一维数组(因)
  tf.keras.layers.Dense(10, activation=tf.nn.relu),
  #10个神经元,使用ReLU算法
  tf.keras.layers.Dense(2, activation=tf.nn.softmax)
  #两种答案,使用softmax算法
])

compile

model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])
#编译并使用Adam算法优化

train

model.fit(x_train, y_train,
          epochs=20,
          batch_size=128)
#进行训练的因和果的数据
#设置训练次数
#设置每次训练的笔数

The complete code at this point:

#!/usr/bin/env python
# -*- coding=utf-8 -*-

import tensorflow as tf

import numpy as np


x1=np.random.random((500,1))
x2=np.random.random((500,1))+1
x_train=np.concatenate((x1, x2))

y1=np.zeros((500,), dtype=int)
y2=np.ones((500,), dtype=int)
y_train=np.concatenate((y1, y2))

model = tf.keras.models.Sequential([
  tf.keras.layers.Dense(10, activation=tf.nn.relu, input_dim=1),
  tf.keras.layers.Dense(10, activation=tf.nn.relu),
  tf.keras.layers.Dense(2, activation=tf.nn.softmax)
])

model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

model.fit(x_train, y_train,
          epochs=20,
          batch_size=128)



operation result:

Here, Epoch 18/20 refers to a total of 20 training times, and the current result is the 18th time.

loss : 0.4544, the loss rate is 0.4544, indicating the gap between the model prediction value and the real value.

accuracy: 0.8440: The current accuracy of the found weights is 0.8440.

Evaluation accuracy

#測試
x_test=np.array([[0.22],[0.31],[1.22],[1.33]])
y_test=np.array([0,0,1,1])

score = model.evaluate(x_test, y_test, batch_size=128)
print("score:",score)

predict

predict = model.predict(x_test)#获取每一个结果的概率
print("predict:",predict)
print("Ans:",np.argmax(predict[0]),np.argmax(predict[1]),np.argmax(predict[2]),np.argmax(predict[3]))

#通过predict_ckasses函数来预测答案
predict2 = model.predict_classes(x_test)
print("predict_classes:",predict2)
print("y_test",y_test[:])

The final full code:

#!/usr/bin/env python
# -*- coding=utf-8 -*-

import tensorflow as tf

import numpy as np


x1=np.random.random((500,1))
x2=np.random.random((500,1))+1
x_train=np.concatenate((x1, x2))

y1=np.zeros((500,), dtype=int)
y2=np.ones((500,), dtype=int)
y_train=np.concatenate((y1, y2))

model = tf.keras.models.Sequential([
  tf.keras.layers.Dense(units=10, activation=tf.nn.relu, input_dim=1),
  tf.keras.layers.Dense(units=10, activation=tf.nn.relu),
  tf.keras.layers.Dense(units=2, activation=tf.nn.softmax)
])

model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

model.fit(x_train, y_train,
          epochs=20,
          batch_size=128)


#測試
x_test=np.array([[0.22],[0.31],[1.22],[1.33]])
y_test=np.array([0,0,1,1])

score = model.evaluate(x_test, y_test, batch_size=128)
print("score:",score)

predict = model.predict(x_test)
print("predict:",predict)
print("Ans:",np.argmax(predict[0]),np.argmax(predict[1]),np.argmax(predict[2]),np.argmax(predict[3]))

predict2 = model.predict_classes(x_test)
print("predict_classes:",predict2)
print("y_test",y_test[:])

operation result:

 

So far, a complete MLP neural network model has been created.

Guess you like

Origin blog.csdn.net/m0_72572822/article/details/130068861