Linear Regression with TensorFlow: Predicting Damage from Attack in a Game
This tutorial walks programmers through building a simple TensorFlow linear regression model that learns the relationship between attack power and damage in a game, covering data preparation, model definition, loss calculation, optimization, training loops, and evaluation with complete Python code examples.
Introduction
This article is aimed at programmers who want to start machine learning with TensorFlow but lack a strong mathematical background. It demonstrates a practical "hello world" linear regression that predicts in‑game damage from attack values.
Preparation
Familiarity with TensorFlow (https://www.tensorflow.org)
Python, TensorFlow and related environment installed (no GPU required)
Basic knowledge of Python or any mainstream programming language
Data
The author collected 30 data points of attack and corresponding damage from the game "King of Glory". Example values are shown in the article.
Code
Import required libraries:
import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as npDefine model variables and placeholders:
w = tf.Variable([1.0], dtype=tf.float64)
b = tf.Variable([0.0], dtype=tf.float64)
# placeholders for input (attack) and output (damage)
x = tf.placeholder(tf.float64)
y = tf.placeholder(tf.float64)
linear_model = x * w + bPrepare training and testing data (scaled by 1/100 to avoid overflow):
atk_train = np.array([289., 389., 409., 569., 649., 659., 739., 759., 779., 799., 819., 169., 264., 291., 311., 324., 331., 344., 351., 411., 431., 511., 591., 691., 791., 209.0]).astype(np.float32) / 100
damage_train = np.array([175., 236., 247., 348., 395., 397., 446., 460., 470., 486., 499., 102., 160., 175., 189., 196., 201., 208., 211., 250., 260., 310., 358., 420., 480., 126.0]).astype(np.float32) / 100
atk_test = np.array([159.0, 169.0, 189.0, 891.]).astype(np.float32) / 100
damage_test = np.array([96.0, 102.0, 114.0, 540.]).astype(np.float32) / 100Define loss function, optimizer and accuracy metric:
loss = tf.reduce_sum(tf.square(linear_model - y))
optimizer = tf.train.GradientDescentOptimizer(0.001)
train = optimizer.minimize(loss)
correct_prediction = tf.abs(tf.cast(linear_model, tf.int32) - tf.cast(y, tf.int32)) < 2
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))Initialize variables and run the training loop (100,000 iterations):
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
for i in range(100000):
sess.run(train, {x: atk_train, y: damage_train})Evaluate model on training and testing sets:
curr_w, curr_b, curr_loss, curr_accuracy = sess.run([w, b, loss, accuracy], {x: atk_train, y: damage_train})
print("w: %s b: %s loss: %s accuracy: %s" % (curr_w, curr_b, curr_loss, curr_accuracy))
curr_w, curr_b, curr_loss, curr_accuracy = sess.run([w, b, loss, accuracy], {x: atk_test, y: damage_test})
print("w: %s b: %s loss: %s accuracy: %s" % (curr_w, curr_b, curr_loss, curr_accuracy))Conclusion
The trained model converges to w ≈ 0.6 and b ≈ 0, meaning damage ≈ 0.6 × attack. This simple linear relationship explains how each point of attack contributes to damage in the game, and the tutorial shows how to obtain such insights using TensorFlow.
Beike Product & Technology
As Beike's official product and technology account, we are committed to building a platform for sharing Beike's product and technology insights, targeting internet/O2O developers and product professionals. We share high-quality original articles, tech salon events, and recruitment information weekly. Welcome to follow us.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.