简体   繁体   English

在 MATLAB 环境中用 Python 代码打开 h5 文件的问题

[英]Issue with opening h5 file with Python code in MATLAB environment

I have an issue with calling Python code in MATLAB.我在 MATLAB 中调用 Python 代码时遇到问题。 My Python code involves predicting the battery state of charge using LSTM with attention ANN based on the inputs sent from MATLAB.我的 Python 代码涉及基于从 MATLAB 发送的输入,使用带有注意力神经网络的 LSTM 预测电池充电状态。 The prediction is then sent back to MATLAB.然后将预测发送回 MATLAB。 I already have previously trained weights and biases saved in an h5 file, which is loaded and used in the Python code.我之前已经将训练好的权重和偏差保存在一个 h5 文件中,该文件在 Python 代码中加载和使用。 Below is the Python code:下面是 Python 代码:

    import pandas as pd
    import numpy as np
    import tensorflow as tf
    from sklearn.model_selection import train_test_split
    from sklearn.metrics import mean_squared_error
    from tensorflow import keras
    from tensorflow.keras import Sequential
    from tensorflow.keras.layers import LSTM
    from tensorflow.keras.layers import Dense
    from tensorflow.keras import optimizers
    import matplotlib.pyplot as plt
    from tensorflow.keras.layers import *
    from tensorflow.keras.models import *
    from tensorflow.keras import backend as K
    import matplotlib.pyplot as plt
    import matplotlib.ticker as ticker
    from sklearn.model_selection import train_test_split
    from tensorflow.keras.callbacks import EarlyStopping
    from tensorflow.keras.callbacks import ModelCheckpoint
    from tensorflow.keras.models import load_model
    from tensorflow.keras.layers import Dropout, InputLayer 
    import h5py

    #to create sequential data
    def create_inout_sequences(input_data, tw):
inout_seq = []
L = len(input_data)
for i in range(L-tw):
    train_seq = input_data[i:i+tw]
    #train_out = output_data[i:i+tw]
    inout_seq.append(train_seq)
return inout_seq 

def search(inputs):
class attention(Layer):
    def __init__(self, return_sequences=True,**kwargs):
        self.return_sequences = return_sequences
        super(attention,self).__init__()

    def build(self, input_shape):

        self.W=self.add_weight(name="att_weight", shape=(input_shape[-1],1),
                               initializer="normal")
        self.b=self.add_weight(name="att_bias", shape=(input_shape[1],1),
                               initializer="zeros")
        super(attention,self).build(input_shape)

    def call(self, x):
        e=(K.dot(x,self.W)+self.b)
        a = K.softmax(e, axis=1)
        output = x*a
        if self.return_sequences:
            return output
        return K.sum(output, axis=1)

    def get_config(self):
        # For serialization with 'custom_objects'
        config = super().get_config()
        config['return_sequences'] = self.return_sequences     
        return config

#convert test data to sequential form
inputs=np.array(inputs)
inputs=np.tile(inputs, (36, 1))
inputs_new=create_inout_sequences(inputs, 35)
inputs_new=np.array(inputs_new)

model1 = Sequential()
model1.add(InputLayer(input_shape=(35,5)))
model1.add((LSTM(22, return_sequences=True)))
model1.add(attention(return_sequences=False))
model1.add(Dense(104, activation="relu"))
model1.add(Dropout(0.2))
model1.add(Dense(1, activation="sigmoid"))

lr_schedule = keras.optimizers.schedules.ExponentialDecay(
initial_learning_rate=0.01,
decay_steps=10000,
decay_rate=0.99)

model1.compile(optimizer=tf.keras.optimizers.Adam(epsilon=1e-08,learning_rate=lr_schedule),loss='mse')

#call previously trained weights
model1.load_weights('SOC_weights.h5')
x=float(model1.predict(inputs_new, batch_size=100,verbose=0))

return x # send prediction to Matlab

Note: I am using Python 3.6, tensorflow version: 2.5, keras version: 2.4.3, h5py version: 3.1.0, cython version: 0.28注意:我使用的是 Python 3.6,tensorflow 版本:2.5,keras 版本:2.4.3,h5py 版本:3.1.0,cython 版本:0.28

I am able to run this code without any error on Python, but have issues when used in MATLAB 2020a... below is my MATLAB code:我能够在 Python 上运行此代码而不会出现任何错误,但是在 MATLAB 2020a 中使用时出现问题......下面是我的 MATLAB 代码:

   pyenv('Version','3.6');
   py.importlib.import_module('tensorflow');
   py.importlib.import_module('testingSOC'); % file containing the Python codes
   inputs=[0.555555556,0.435139205,0.68313128,0.499987472,0.241225578];% test inputs
   SOC_output=py.testingSOC.search(inputs)

Below is the error received on Matlab:以下是在 Matlab 上收到的错误:

    Error using training>load_weights (line 2312)
    Python Error: ImportError: `load_weights` requires h5py when loading weights from HDF5.
    Error in testingSOC>search (line 87)

the error looks like h5py is not identified by MATLAB, so I have tried reinstalling h5py by using the command prompt (I am using Windows 10):该错误看起来像 MATLAB 未识别出 h5py,因此我尝试使用命令提示符重新安装 h5py(我使用的是 Windows 10):

    pip uninstall h5py
    pip install h5py

but no changes...但没有变化...

I have also tried with tensorflow version: 2.2, keras version 2.4.3, h5py version 2.10 and cython version 0.29 but still get the same error.我也尝试过 tensorflow 版本:2.2、keras 版本 2.4.3、h5py 版本 2.10 和 cython 版本 0.29,但仍然出现相同的错误。

I would really appreciate if you guys can provide an insight in solving this issue, and if there are any fundamental parts that I have missed.如果你们能提供解决此问题的见解,以及我遗漏的任何基本部分,我将不胜感激。 I would be glad to share more details if required.如果需要,我很乐意分享更多细节。

Thanks!谢谢!

Thanks to @TimRoberts for pointing out about including 'py.importlib.import_module('h5py')' which helped me in resolving this issue.Below is my solution, for those who would like to refer:感谢@TimRoberts 指出包含 'py.importlib.import_module('h5py')' 这帮助我解决了这个问题。以下是我的解决方案,供想参考的人参考:

When I included 'py.importlib.import_module('h5py')' in my matlab codes, I received the following error:当我在我的 matlab 代码中包含 'py.importlib.import_module('h5py')' 时,我收到以下错误:

    Error using h5>init h5py.h5 (line 1) 
    Python Error: ImportError: DLL load failed: The specified procedure could not be found.

It looks like Python environment seems to use Matlab's h5 library in my case, which does not have the same features as Python's h5 library...I found that there is an option of running Python codes as a separate process which seems to be working for me (as seen in this link): https://www.mathworks.com/help/matlab/matlab_external/out-of-process-execution-of-python-functionality.html?searchHighlight=out%20of%20process%20python&s_tid=srchtitle在我的例子中,看起来 Python 环境似乎使用了 Matlab 的 h5 库,它与 Python 的 h5 库没有相同的功能......我发现有一个选项可以将 Python 代码作为一个单独的进程运行,这似乎是为我(如本链接所示): https : //www.mathworks.com/help/matlab/matlab_external/out-of-process-execution-of-python-functionality.html?searchHighlight=out%20of%20process%20python&s_tid =srchtitle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM