代码之家  ›  专栏  ›  技术社区  ›  Tobias von Falkenhayn

张量流的简单分类

  •  0
  • Tobias von Falkenhayn  · 技术社区  · 6 年前

    我有以下问题:

    • 我有一个覆盆子皮机器人,它使用四个传感器(左,前,右,后)。
    • 机器人可以做以下动作:向前跑,向左拐,向右拐,向后退。
    • 根据传感器的数据,我将“训练”机器人做这些动作。

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

    • 如果传感器数据=[2,1,0,1]=>向左移动
    • 如果传感器数据=[4,0,1,1]=>向左移动
    • 如果传感器数据=[0,2,0,0]=>向前移动
    • 如果传感器数据=[0,0,0,1]=>向后移动
    • ……在这里输入更多数据..

    现在训练结束后,机器人应该可以预测下一步的动作,比如:

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

    我的第一个想法是 张量流 为了解决这个问题,我无法找到实现这种(简单的)预测算法的最佳方法,因为大多数教程都是关于图像和语音识别的。

    如果有人能用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
    
    2 回复  |  直到 6 年前
        1
  •  1
  •   William D. Irons    6 年前

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

        2
  •  0
  •   teclnol    6 年前

    我推荐一个简单的神经网络。虽然它们用于图像识别和语音识别,但它们可以用于许多事情。 它的本质是你给它提供了大量的训练数据(输入,以及期望的输出),它会根据这些数据来决定该怎么做。你的情况可能是这样的:

    输入-----所需输出[如果最高值是第一个:左,第二个:前进,第三个:后退,第四个:右]

    [1/2,1/4,0,1/4]>[1,0,0,0]

    [1,0,1/4,1/4]>[1,0,0,0]

    [0,1/2,0,0]>[0,1,0,0]

    [0,0,0,1/4]>[0,0,1,0]

    我使用分数是因为这是一个标准过程,它的值可以介于0和1之间,也可以介于-1和1之间。 有了足够的例子,它将学会做什么。