我们正在使用CNTK的CNTK构建一个LSTM网络,但是根据C#文档的当前级别,我们发现很难确定输入的适当形状/尺寸。
我们有一个时间序列,每个时间t都有一个值(一个数字),我们希望使用该时间序列的前744个值的序列来使用LSTM进行预测。此外,我们想要制作一个包含25个序列的小批量CNTK.InputVariable的形状应该是什么样子:
744
1 1
2 25
或
1
1744
2 25
…然后,如果我们在每个时间t有两个值而不是一个值,那么CNTK.InputVariable形状会是什么样子呢?
发布于 2019-04-25 23:31:17
如果你使用递归网络(LSTM,GRU),那么你需要知道什么是静态和动态轴。静态轴用于描述输入数据表单(在第一种情况下,它是一个秩为1、大小为1的向量:new int {1})。动态轴用于指定输入数据的序列(在您的示例中为744,长度可变)(在您的示例中为new int {1})。要指示应将动态轴用于序列,请在输入参数dynamicAxes:new[] { Axis.DefaultBatchAxis() }中指定
var inputDimension = 1; //for two values is 2 etc.
var inputShape = new { inputDimension };
var input = Variable.InputVariable(inputShape, DataType.Double, "input", new[] { Axis.DefaultBatchAxis() });并确保正确地准备小批(创建一个小批的示例):
var device = DeviceDescriptor.CPUDevice;
var inputDimension = 1;
var outputDimension = 1;
var minibatchSize = 25;
var oneMinibatchFeaturesData = new List<List<double[]>>(minibatchSize)
{
new List<double[]> //first sequence
{
new double[] { 23 },//t=1. Array.Length = inputDimension
new double[] { 25 },//t=2
//...
new double[] { 65 },//t=744
},
new List<double[]> //second seqeunce
{
new double[] { 76 }, //t=1
new double[] { 236 },//t=2
//...
new double[] { 87 }, //t=744
},
//...
new List<double[]> //twenty fifth sequence
{
new double[] { 9 }, //t=1
new double[] { 2 },//t=2
//...
new double[] { 90 }, //t=744
},
};
var oneMinibatchLabelsData = new List<double[]>(minibatchSize)
{
new double[] { 1 },//label of first sequence. Array.Length = outputDimension
new double[] { 5 },//label of second sequence
//...
new double[] { 3 }//label of twenty fifth sequence
};
var features = Value.CreateBatchOfSequences(new[] { inputDimension }, oneMinibatchFeaturesData.Select(sequence => sequence.SelectMany(value => value)), device);
var labels = Value.CreateBatch(new[] { outputDimension }, oneMinibatchLabelsData.SelectMany(value => value), device);序列的长度可以是任意的。一个小批量可能包含不同长度的序列。
LSTM很难在这种长度的序列上进行训练。如果序列的长度始终是744,那么您可能应该使用输入维度为744的简单FNN。
发布于 2019-08-02 17:48:25
深入研究CNTK的读者是我推荐的!
@Stanislav Grigorev是正确的。
输入维度完全取决于您的数据集。例如,示例中的ATIS如下所示:

代码示例可以在here中找到。
数据通过读取器读入:
IList<StreamConfiguration> streamConfigurations = new StreamConfiguration[]
{
new StreamConfiguration(featuresName, inputDim, true, "S0"),
// new StreamConfiguration(featuresName, inputDim, true, "S1"), // Not used in the old example.
new StreamConfiguration(labelsName, numOutputClasses, false, "S2")
};以及TextFormatMinibatchSource的读数:
var minibatchSource = MinibatchSource.TextFormatMinibatchSource(
Path.Combine(DataFolder, "Train.ctf"),
streamConfigurations,
MinibatchSource.InfinitelyRepeat,
true);
var featureStreamInfo = minibatchSource.StreamInfo(featuresName);
var labelStreamInfo = minibatchSource.StreamInfo(labelsName);然后,while循环中的这一行:
var minibatchData = minibatchSource.GetNextMinibatch(minibatchSize, device);读取每个小批次。这对于任何阅读代码的人来说都是显而易见的,但为了说明读取数据的方式,我提供了这个示例。
代码示例中给出了数据集参数:
const int inputDim = 2000;
const int numOutputClasses = 5;这些数字是正确的,这一点很重要!
我已经建立了一个网站:http://www.cntking.com/,试图在C#和CNTK上取得进展,我认为这是一个非常被低估的机器学习语言C#。
https://stackoverflow.com/questions/52219993
复制相似问题