我遇到了以下问题:
- 我有一个使用四个传感器(左、前、右、后)的树莓派机器人。
- 机器人可以执行以下动作:向前行驶,向左转,向右转,向后移动。
- 基于传感器数据,我将“训练”机器人执行这些动作。
因此,用于训练机器人的基本输入看起来像这样:
- 如果传感器数据 = [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 tfimport 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 namesCSV_COLUMN_NAMES = ['Front', 'Back', 'Left', 'Right', 'Move']# The moves (results) of the estimationMOVES = ['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 = 1000def 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 dsdef 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 scriptif __name__ == '__main__': tf.logging.set_verbosity(tf.logging.INFO) tf.app.run(main)
CSV文件看起来像这样
Front,Back,Left,Right,Move100,100,100,100,0150,150,150,150,0100,200,100,200,0110,110,200,200,0200,100,200,100,0140,150,200,140,0120,120,120,170,0140,170,170,120,0170,150,130,140,0190,190,100,130,0110,150,160,110,0160,170,110,100,0170,140,160,110,0180,160,110,120,0130,200,110,190,0120,150,160,110,0160,180,120,100,0170,140,140,110,0180,110,110,120,0110,200,140,190,010,100,10,10,140,150,40,40,110,200,10,20,120,110,20,20,110,100,20,10,110,150,20,40,120,120,10,10,130,170,40,20,140,150,30,40,140,190,30,30,130,150,40,10,110,170,30,40,120,140,20,10,130,160,20,20,120,200,10,40,110,150,40,10,120,120,30,40,120,120,20,20,130,160,20,10,110,100,10,10,110,100,100,10,240,150,140,40,210,200,160,20,220,110,120,20,210,100,120,10,210,150,180,40,220,120,110,10,230,170,140,20,240,150,130,40,240,190,130,30,230,150,140,10,210,170,150,40,220,140,120,10,230,160,120,20,220,200,170,40,210,160,50,20,240,100,70,40,220,160,60,10,220,100,90,20,210,100,10,10,340,150,40,100,310,200,30,120,320,110,20,120,310,100,20,110,310,150,20,140,320,120,10,110,330,170,40,120,340,150,30,140,340,190,30,130,330,150,40,110,310,170,50,140,320,140,20,110,330,160,20,120,320,200,40,140,330,150,40,70,310,150,40,60,310,140,10,90,330,140,30,80,320,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个预期输出值。