Home > Blockchain >  Problems with LSTM neural network
Problems with LSTM neural network

Time:08-16

I am designing a neural network using LSTM with keras library for what I think is a sequence classification problem. But I am having problems when fitting the model. This is the problematic code:

# create LSTM model
model = Sequential()
model.add(LSTM(4,activation='relu', input_shape=(len(x),1)))
model.add(Dense(1))
model.compile(optimizer='adam', loss='mse')
model.fit(x,y,epochs = 100, batch_size = 1,verbose=2)

When I execute it, I get the following error:

> File "_pydevd_bundle/pydevd_cython.pyx", line 532, in
> _pydevd_bundle.pydevd_cython.PyDBFrame._handle_exception   File "C:\Program
> Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.3568.0_x64__qbz5n2kfra8p0\lib\linecache.py",
> line 30, in getline
>     lines = getlines(filename, module_globals)   File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.3568.0_x64__qbz5n2kfra8p0\lib\linecache.py",
> line 46, in getlines
>     return updatecache(filename, module_globals)   File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.3568.0_x64__qbz5n2kfra8p0\lib\linecache.py",
> line 137, in updatecache
>     lines = fp.readlines()   File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.3568.0_x64__qbz5n2kfra8p0\lib\codecs.py",
> line 322, in decode
>     (result, consumed) = self._buffer_decode(data, self.errors, final) UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa1 in position
> 1465: invalid start byte
> Traceback (most recent call last):   File "C:\Program
> Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.3568.0_x64__qbz5n2kfra8p0\lib\runpy.py",
> line 197, in _run_module_as_main
>     return _run_code(code, main_globals, None,   File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.3568.0_x64__qbz5n2kfra8p0\lib\runpy.py",
> line 87, in _run_code
>     exec(code, run_globals)   File "c:\program files\microsoft visual studio\2022\community\common7\ide\extensions\microsoft\python\core\debugpy\__main__.py",
> line 45, in <module>
>     cli.main()   File "c:\program files\microsoft visual studio\2022\community\common7\ide\extensions\microsoft\python\core\debugpy/..\debugpy\server\cli.py",
> line 444, in main
>     run()   File "c:\program files\microsoft visual studio\2022\community\common7\ide\extensions\microsoft\python\core\debugpy/..\debugpy\server\cli.py",
> line 285, in run_file
>     runpy.run_path(target_as_str, run_name=compat.force_str("__main__"))   File "C:\Program
> Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.3568.0_x64__qbz5n2kfra8p0\lib\runpy.py",
> line 288, in run_path
>     return _run_module_code(code, init_globals, run_name,   File "C:\Program
> Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.3568.0_x64__qbz5n2kfra8p0\lib\runpy.py",
> line 97, in _run_module_code
>     _run_code(code, mod_globals, init_globals,   File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.3568.0_x64__qbz5n2kfra8p0\lib\runpy.py",
> line 87, in _run_code
>     exec(code, run_globals)   File "C:\Users\34670\Desktop\UBU\Doctorado\Python\Redes
> Neuronales\Balanceador01\Balanceador01.py", line 54, in <module>  
> File "_pydevd_bundle/pydevd_cython.pyx", line 1366, in
> _pydevd_bundle.pydevd_cython.SafeCallWrapper.__call__   File "_pydevd_bundle/pydevd_cython.pyx", line 322, in
> _pydevd_bundle.pydevd_cython.PyDBFrame.trace_exception   File "_pydevd_bundle/pydevd_cython.pyx", line 452, in
> _pydevd_bundle.pydevd_cython.PyDBFrame.handle_user_exception   File "_pydevd_bundle/pydevd_cython.pyx", line 535, in
> _pydevd_bundle.pydevd_cython.PyDBFrame._handle_exception   File "C:\Program
> Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.3568.0_x64__qbz5n2kfra8p0\lib\linecache.py",
> line 30, in getline
>     lines = getlines(filename, module_globals)   File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.3568.0_x64__qbz5n2kfra8p0\lib\linecache.py",
> line 46, in getlines
>     return updatecache(filename, module_globals)   File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.3568.0_x64__qbz5n2kfra8p0\lib\linecache.py",
> line 137, in updatecache
>     lines = fp.readlines()   File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.3568.0_x64__qbz5n2kfra8p0\lib\codecs.py",
> line 322, in decode
>     (result, consumed) = self._buffer_decode(data, self.errors, final) UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa1 in position
> 1465: invalid start byte

Any idea of what could be problem? I am sure that for example the input_shape of the LSTM layer is ok (I have used reshape to convert it to 3D), but I don't understand the error message so I cannot go on.

The input data comes from this csv file . The first column (number zero) is the time frame, which is not needed as this is a sequence along time. This is how I process the data, very straight forward:

# load both voltages and balanceadorOn values
x = pd.read_csv('5 abril 2022.csv', usecols=[1], engine='python')
x = x.values
y = pd.read_csv('5 abril 2022.csv', usecols=[2], engine='python')
y = y.values

# normalize the voltages
scaler = MinMaxScaler(feature_range=(0, 1))
x = scaler.fit_transform(x)

# Reshape input data to 3D for the LSTM
x = x.reshape(1,len(x),1)

CodePudding user response:

The problem was on the code related with the import of tensorflow, I found this whil looking deeply for this type of error along stack overflow posts. Now I am using the following importing and it works correctly (the difference is that it has python between tensorflow and keras):

import pandas as pd
import tensorflow as tf
from tensorflow.python.keras import Sequential
from tensorflow.python.keras.layers import Dense
from tensorflow.python.keras.layers import LSTM
  • Related