【问题标题】:Simple classification in TensorFlowTensorFlow 中的简单分类
【发布时间】:2018-12-22 00:55:40
【问题描述】:

我有以下问题:

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

所以训练机器人的基本输入如下所示:

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

现在,经过训练,机器人应该可以预测下一步行动,例如:

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

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

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

所以主要问题是:

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


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

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?如果您对值进行硬编码,这将是一个巨大的 switch case 或 if / else(如果值上升到 4,则为 625,如果上升到 9,则为 10000),因此不推荐使用 def。如果你想训练机器人,你需要它可以运行预测的模式/标准。哪些特征将决定方向?
  • 从我从您的示例中看到的,您可以根据 [Left, Forward, Right, Back] 条目中哪个值较高来确定方向,如果是这种情况,您只需要实现一个边缘情况的一些规则(2等于,3等于,全等于),然后将较高的值作为方向。

标签: python algorithm math tensorflow machine-learning


【解决方案1】:

看一下 Tensorflow 的 iris 示例:

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 个预期输出值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-07
    • 2013-10-05
    • 1970-01-01
    • 2021-11-19
    • 1970-01-01
    • 2019-01-06
    • 1970-01-01
    相关资源
    最近更新 更多