ILD

training a simple model using tensorflow
作者:Yuan Jianpeng 邮箱:yuanjp89@163.com
发布时间:2026-3-27 站点:Inside Linux Development

安装TensorFlow,这里直接使用当前用户安装,不需要使用root用户安装。

$ pip install tensorflow


初试牛刀

训练一个线性回归(Linear Regression)模型,计算华氏温度

F = 9/5 * C + 32


第一步,生成训练数据:

import numpy as np
import tensorflow as tf

# Independent variables
temp_c = []

# Dependent variables
temp_f = []

# Fill the arrays with calculated values
for c in range(0, 16):
    f = 32 + 1.8 * c
    temp_c.append(float(c))
    temp_f.append(float(f))

# Input array
input_array = np.array(temp_c, dtype=float)

# Correct values for the input array
output_array = np.array(temp_f, dtype=float)


第二步,创建模型

model = tf.keras.Sequential()
model.add(tf.keras.layers.Input(shape=[1]))
model.add(tf.keras.layers.Dense(units=1))
model.compile(optimizer=tf.keras.optimizers.SGD(), loss=tf.keras.losses.MeanSquaredError())


第三步,训练

model.fit(input_array, output_array, epochs=1000)


第4步,使用模型预测

# Temperature in Celsius
temp_c = [0,1,2,3,4,5]

# Predicted temperature in Fahrenheit
temp_f = model.predict(np.array(temp_c))

print(temp_f)


第5步,保存模型

model.save("c_to_f_conversion.keras")


参数调优

将上面的代码,组合成一个simple.py运行:

$ ./simple.py
WARNING: All log messages before absl::InitializeLog() is called are written to STDERR
I0000 00:00:1774608384.167101  402252 cudart_stub.cc:31] Could not find cuda drivers on your machine, GPU will not be used.
I0000 00:00:1774608384.209737  402252 cpu_feature_guard.cc:227] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
WARNING: All log messages before absl::InitializeLog() is called are written to STDERR
I0000 00:00:1774608385.289595  402252 cudart_stub.cc:31] Could not find cuda drivers on your machine, GPU will not be used.
E0000 00:00:1774608385.466528  402252 cuda_platform.cc:52] failed call to cuInit: INTERNAL: CUDA error: Failed call to cuInit: UNKNOWN ERROR (303)
Epoch 1/100
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 118ms/step - loss: 1658.9235
Epoch 2/100
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 27ms/step - loss: 712.2487
Epoch 3/100
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 27ms/step - loss: 408.5208
。。。
Epoch 96/100
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 27ms/step - loss: 97.2175
Epoch 97/100
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 25ms/step - loss: 96.1640
Epoch 98/100
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 27ms/step - loss: 95.1219
Epoch 99/100
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 27ms/step - loss: 94.0912
Epoch 100/100
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 28ms/step - loss: 93.0716
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 35ms/step
[[13.676559]
 [17.256033]
 [20.835506]
 [24.41498 ]
 [27.994453]
 [31.573927]]


预测结果和实际值偏差很大,请教有经验的同事,loss没有降下来,原因是训练次数不够,将

 epochs=100改成1000,结果就准确了,

[[31.864792]

 [33.67792 ]

 [35.491055]

 [37.304184]

 [39.117313]

 [40.930443]]


DeepSeek优化

deepseek给出了优化的代码

#!/usr/bin/python3

import numpy as np
import tensorflow as tf

# 固定种子,保证可重复性
tf.random.set_seed(42)
np.random.seed(42)

# 数据
celsius = np.arange(0, 16, dtype=np.float32)
fahrenheit = 32 + 1.8 * celsius

# 模型
model = tf.keras.Sequential([
    tf.keras.layers.Dense(1, input_shape=[1])
])

# 编译:使用 SGD,小学习率
model.compile(optimizer=tf.keras.optimizers.SGD(learning_rate=0.01),
              loss='mse')

# 训练,打印损失
history = model.fit(celsius, fahrenheit, epochs=2000, verbose=1)

# 输出最终权重
w, b = model.layers[0].get_weights()
print(f"训练后权重: w={w[0][0]:.6f}, b={b[0]:.6f}")

# 预测
test_c = np.array([0, 1, 2, 3, 4, 5], dtype=np.float32)
pred = model.predict(test_c).flatten()
true = 32 + 1.8 * test_c
print("\n预测值:", pred)
print("真实值:", true)
print("最大误差:", np.max(np.abs(pred - true)))


可以打印出w和b值。

训练后权重: w=1.800057, b=31.999413

预测值: [31.999413 33.79947  35.599525 37.399582 39.199642 40.9997  ]

真实值: [32.  33.8 35.6 37.4 39.2 41. ]

最大误差: 0.0005874634


w值和b值,就是线性回归的, y= w *x + b

可以看到w和b接近真实的9/5和32。误差也非常小。


这里有两个值需要调优:

learning_rate=0.01

epochs=2000


larning_rate太小,则学习的很慢,太大则误差大。

epochs太小,则训练不够。


参考:

https://vitalyzhukov.com/en/tensorflow-create-train-and-use-simple-model


Copyright © linuxdev.cc 2017-2024. Some Rights Reserved.