gpt4 book ai didi

python - TensorFlow 中的简单分类

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:09:53 24 4
gpt4 key购买 nike

我有以下问题:

  • 我有一个使用四个传感器(左、前、右、后)的树莓派机器人。
  • 机器人可以做以下 Action :向前跑、左转、右转、后退。
  • 根据传感器数据,我将“训练”机器人做 Action 。

因此,训练机器人的基本输入如下所示:

  • 如果传感器数据 = [2, 1, 0, 1] => 向左移动
  • 如果传感器数据 = [4, 0, 1,1] => 向左移动
  • 如果传感器数据 = [0, 2, 0, 0] => 向前移动
  • if sensor data = [0, 0, 0, 1] => 向后移动
  • .. 在此处输入更多数据..

现在在训练之后,机器人应该可以像这样预测下一步行动:

如果传感器数据 = [3, 3, 2, 1] => 进行预测移动。

我的第一个想法是使用 TensorFlow 来解决这个问题,但是我找不到实现这个(有点简单的)预测算法的最佳方法,因为大多数教程是关于图像和语音识别的。

如果有人能用 Python 向我展示如何使用 TensorFlow 完成此操作的简短示例,那就太好了。

所以主要问题是:

我如何实现一种算法,该算法将具有四个值的数组列表作为输入,然后预测给定的输出(一个变量可以有四种状态)。


解决方案:我设法找到了一个解决方案(它使用带有 TensorFlow 和 Pandas 的 Python 3):

import tensorflow as tf
import pandas as pd

# Path to the directory where model data should be saved.
MODEL_PATH = "model"
# Path to the training data file.
TRAIN_DATA_PATH = "movement_training_data.csv"

# The csv column names
CSV_COLUMN_NAMES = ['Front', 'Back', 'Left', 'Right', 'Move']
# The moves (results) of the estimation
MOVES = ['Forward', 'Back', 'Left', 'Right']

# Defines the batch size of data taken for each training step.
batch_size = 100

# Defines how many training steps should be done.
# Weights and biases wll be adjusted after each step.
train_steps = 1000


def main(argv):
# Reads the csv data and assigns column names. The first line is the header line.
train_data = pd.read_csv(TRAIN_DATA_PATH, names=CSV_COLUMN_NAMES, header=0)

# Generates a train_features and a train_label data frame.
train_features, train_labels = train_data, train_data.pop('Move')

# Add feature columns (all numeric).
feature_columns = []
for key in train_features.keys():
feature_columns.append(tf.feature_column.numeric_column(key=key))

# Create classifier for a deep neural network (DNN)
classifier = tf.estimator.DNNClassifier(
# Set the model directory.
model_dir=MODEL_PATH,
# Set the feature columns.
feature_columns=feature_columns,
# Two hidden layers of 10 nodes each.
hidden_units=[10, 10],
# The model must choose between 5 classes (which in this case consist of one label each).
n_classes=4)

# Train the Model.
classifier.train(
input_fn=lambda: train_input(train_features, train_labels),
steps=train_steps)

# Test prediction data.
data_to_predict = {
'Front': [115, 42, 30, 21],
'Back': [142, 151, 120, 121],
'Left': [145, 23, 81, 15],
'Right': [155, 25, 43, 192],
}

predictions = classifier.predict(
input_fn=lambda: eval_input(data_to_predict, labels=None))

for prediction_dict in predictions:
# 0 = Forward, 1 = Back, 2 = Left, 3 = Right
class_id = prediction_dict['class_ids'][0]
probability = prediction_dict['probabilities'][class_id]

print(str(class_id) + ": " + str(probability))


def train_input(features, labels):
# Convert the inputs to a data set.
ds = tf.data.Dataset.from_tensor_slices((dict(features), labels))

# Shuffle, repeat, and batch the examples.
ds = ds.shuffle(1000).repeat().batch(batch_size)

# Return the data set.
return ds


def eval_input(features, labels):
features = dict(features)

if labels is None:
# No labels, use only features.
inputs = features
else:
inputs = (features, labels)

# Convert the inputs to a data set.
ds = tf.data.Dataset.from_tensor_slices(inputs)

# Batch the examples
ds = ds.batch(batch_size)

# Return the data set.
return ds


# Execute TensorFlow program if started directly from script
if __name__ == '__main__':
tf.logging.set_verbosity(tf.logging.INFO)
tf.app.run(main)

CSV 看起来像

Front,Back,Left,Right,Move
100,100,100,100,0
150,150,150,150,0
100,200,100,200,0
110,110,200,200,0
200,100,200,100,0
140,150,200,140,0
120,120,120,170,0
140,170,170,120,0
170,150,130,140,0
190,190,100,130,0
110,150,160,110,0
160,170,110,100,0
170,140,160,110,0
180,160,110,120,0
130,200,110,190,0
120,150,160,110,0
160,180,120,100,0
170,140,140,110,0
180,110,110,120,0
110,200,140,190,0
10,100,10,10,1
40,150,40,40,1
10,200,10,20,1
20,110,20,20,1
10,100,20,10,1
10,150,20,40,1
20,120,10,10,1
30,170,40,20,1
40,150,30,40,1
40,190,30,30,1
30,150,40,10,1
10,170,30,40,1
20,140,20,10,1
30,160,20,20,1
20,200,10,40,1
10,150,40,10,1
20,120,30,40,1
20,120,20,20,1
30,160,20,10,1
10,100,10,10,1
10,100,100,10,2
40,150,140,40,2
10,200,160,20,2
20,110,120,20,2
10,100,120,10,2
10,150,180,40,2
20,120,110,10,2
30,170,140,20,2
40,150,130,40,2
40,190,130,30,2
30,150,140,10,2
10,170,150,40,2
20,140,120,10,2
30,160,120,20,2
20,200,170,40,2
10,160,50,20,2
40,100,70,40,2
20,160,60,10,2
20,100,90,20,2
10,100,10,10,3
40,150,40,100,3
10,200,30,120,3
20,110,20,120,3
10,100,20,110,3
10,150,20,140,3
20,120,10,110,3
30,170,40,120,3
40,150,30,140,3
40,190,30,130,3
30,150,40,110,3
10,170,50,140,3
20,140,20,110,3
30,160,20,120,3
20,200,40,140,3
30,150,40,70,3
10,150,40,60,3
10,140,10,90,3
30,140,30,80,3
20,200,40,70,3

最佳答案

看看 Tensorflow 的鸢尾花示例:

https://www.tensorflow.org/versions/r1.5/get_started/get_started_for_beginners

https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/learn/iris.py

这不是基于虹膜图像的训练,它是基于输入 csv 文件进行训练的,该文件采用 4 个值和 1 个预期输出值。

关于python - TensorFlow 中的简单分类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51328575/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com