# How to define and get weight value in LSTM

Hi guys

So I had a project with control system. I wanna get a math model i.e state space model or transfer function.

So after read a few research, I found out that we need to know the weight.

Before I do something like this. Get the sensor data as database. Do training in Matlab with LSTM feature and predicted. But I can’t define weight and get the final weight that its uses in training model.

Do you guys know how to do it with tensorflow?

Thank you

What you are trying to achieve is known as system identification. You need to get training data, then train the model architecture you built to emulate that system.

## Get training data:

In MATLAB/Simulink, use the transfer function block and fed it from constant block. To save the data use to workspace block.

1. Discretize the “to workspace” block such that the training data is not too much.

2. Add the “to workspace” block at the output.

3. Run the system to get output sequence.

4. Go to the workspace and save the output variable to excel such that each sample is in a column.

5. Add extra column for the input (the value you set in the constant block).

6. Repeat with different values.

You can write the model with sequential API as it is not complex.

Go to the workspace, get the input and output and store them

Hi @naisa_xie ,

you could use the set_weights() and get_weights() function on TF layers.

``````# https://www.tensorflow.org/api_docs/python/tf/keras/layers/LSTM

# Create a little TF Sequential LSTM Model
# (e.g: 2 Layers to iterate over weights)
lstm_model = tf.keras.models.Sequential([
tf.keras.layers.LSTM(32),
tf.keras.layers.Dense(1)
])

# Feed some random inputs through the network
input = tf.random.normal([32, 10, 8])
output = lstm_model(input)

# Get each layers weight's
for idx in range(len(lstm_model.layers)):
print('Layer ', idx)
for weights in lstm_model.layers[idx].get_weights():
print(weights.shape)
#print(weights)
``````

Outputs:
Layer 0
(8, 128)
(32, 128)
(128,)
Layer 1
(32, 1)
(1,)

Cheers,
Dennis