Click here to Skip to main content
15,901,426 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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

# univariate lstm example
from numpy import array
from keras.models import Sequential
from keras.layers import LSTM
from keras.layers import Dense
# split a univariate sequence into samples
def split_sequence(sequence, n_steps):
	X, y = list(), list()
	for i in range(len(sequence)):
		# find the end of this pattern
		end_ix = i + n_steps
		# check if we are beyond the sequence
		if end_ix > len(sequence)-1:
		# gather input and output parts of the pattern
		seq_x, seq_y = sequence[i:end_ix], sequence[end_ix]
	return array(X), array(y)
# define input sequence
raw_seq = [10, 20, 30, 40, 50, 60, 70, 80, 90]
# choose a number of time steps
n_steps = 3
# split into samples
X, y = split_sequence(raw_seq, n_steps)
# reshape from [samples, timesteps] into [samples, timesteps, features]
n_features = 1
X = X.reshape((X.shape[0], X.shape[1], n_features))
# define model
model = Sequential()
model.add(LSTM(50, activation='relu', input_shape=(n_steps, n_features)))
model.compile(optimizer='adam', loss='mse')
# fit model, y, epochs=200, verbose=0)
# demonstrate prediction
x_input = array([70, 80, 90])
x_input = x_input.reshape((1, n_steps, n_features))
yhat = model.predict(x_input, verbose=0)

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.InputLayer(theShape2));  // can't get this to work
            model.add(keras.layers.LSTM(50, keras.activations.Relu));  
            model.compile(optimizer: keras.optimizers.Adam(), loss: keras.losses.MeanSquaredError());                   
  , y, epochs: 200, verbose: 0);
Updated 27-Sep-22 4:51am
Richard MacCutchan 26-Sep-22 15:28pm    
You should use your debugger to find out exactly where that exception gets thrown.

1 solution

Thank you for your response. Actually, I do know where the exception is getting thrown. It is getting thrown in the fit() method - presumably after control goes to Tensorflow. I just can't get a grip on what "sequence" has no elements. I know x and y have their elements. As I have checked those just before the fit(). What makes this so puzzling is that I am basically just copying the code from the other site and it works fine in Python.
Share this answer
Richard Deeming 28-Sep-22 4:20am    
If you want to reply to a comment, click the "Reply" button next to that comment. Do not post your comment as a "solution" to your question.
Member 14543240 29-Sep-22 12:25pm    
Thank you for the information. It was a newby mistake.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900