I am somewhat new to deep learning and I am trying to convert an example from Python to C#. I thought this would be a simple task but that has not been the case.
First, this is the code I am trying to convert this python example that works well into C# code. Here is the example (copied from
https://machinelearningmastery.com/how-to-develop-lstm-models-for-time-series-forecasting/...
from numpy import array
from keras.models import Sequential
from keras.layers import LSTM
from keras.layers import Dense
def split_sequence(sequence, n_steps):
X, y = list(), list()
for i in range(len(sequence)):
end_ix = i + n_steps
if end_ix > len(sequence)-1:
break
seq_x, seq_y = sequence[i:end_ix], sequence[end_ix]
X.append(seq_x)
y.append(seq_y)
return array(X), array(y)
raw_seq = [10, 20, 30, 40, 50, 60, 70, 80, 90]
n_steps = 3
X, y = split_sequence(raw_seq, n_steps)
n_features = 1
X = X.reshape((X.shape[0], X.shape[1], n_features))
model = Sequential()
model.add(LSTM(50, activation='relu', input_shape=(n_steps, n_features)))
model.add(Dense(1))
model.compile(optimizer='adam', loss='mse')
model.fit(X, y, epochs=200, verbose=0)
x_input = array([70, 80, 90])
x_input = x_input.reshape((1, n_steps, n_features))
yhat = model.predict(x_input, verbose=0)
print(yhat)
What I have tried:
I have tried converting all of this code to C# and hit two different barriers. First, the input_shape is apparently not available in the C# libraries I am using. That is not critical as the shape is not necessary to defined; it can be inferred from the input data. The ERROR I am getting is that I cannot fit the data even though everything looks identical. Here is my error and below is my code. Any help would be greatly appreciated.
Unhandled exception.
System.InvalidOperationException: Sequence contains no elements
using System;
using Tensorflow;
using Tensorflow.NumPy;
using static Tensorflow.Binding;
using static Tensorflow.KerasApi;
namespace LSTM_Test
{
internal class Program
{
static void Main(string[] args)
{
var x = np.array(new float[,] { { 10.0f, 20.0f, 30.0f }, { 20.0f, 30.0f, 40.0f }, { 30.0f, 40.0f, 50.0f }, { 40.0f, 50.0f, 60.0f }, { 50.0f, 60.0f, 70.0f }, { 60.0f, 70.0f, 80.0f }, { 70.0f, 80.0f, 90.0f }, { 80.0f, 90.0f, 100.0f }, { 90.0f, 100.0f, 110.0f } });
var y = np.array(new float[] { 40.0f, 50.0f, 60.0f, 70.0f, 80.0f, 90.0f, 100.0f, 110.0f, 120.0f });
Tensorflow.Shape theShape = new Tensorflow.Shape(9, 3, 1);
x = np.reshape(x, theShape);
Tensorflow.Shape theShape2 = new Tensorflow.Shape(3, 1);
var input = keras.Input(theShape2);
var model = keras.Sequential();
model.add(keras.layers.LSTM(50, keras.activations.Relu));
model.add(keras.layers.Dense(1));
model.compile(optimizer: keras.optimizers.Adam(), loss: keras.losses.MeanSquaredError());
model.fit(x, y, epochs: 200, verbose: 0);
return;
}
}
}