如何在Azure ML Studio Designer中训练自定义Tensorflow模型

hfyxw5xn  于 2022-11-16  发布在  其他
关注(0)|答案(1)|浏览(142)

我目前正在试用Azure ML生态系统的不同架构。目前,我正在测试Azure ML Studio Designer。
当我使用“创建Python模型”组件创建自定义Tensorflow模型时,当我运行设计器管道时,我收到一个错误,提示未找到Tensorlfow。
错误:

---------- Start of error message from Python interpreter ----------
Got exception when importing script: 'No module named 'tensorflow''.
---------- End of error message from Python  interpreter  ----------

任务中的脚本:

# The script MUST define a class named AzureMLModel.
# This class MUST at least define the following three methods: "__init__", "train" and "predict".
# The signatures (method and argument names) of all these methods MUST be exactly the same as the following example.

# Please do not install extra packages such as "pip install xgboost" in this script,
# otherwise errors will be raised when reading models in down-stream modules.

import pandas as pd
import numpy as np

import tensorflow as tf
from sklearn.preprocessing import OneHotEncoder

class AzureMLModel:
    # The __init__ method is only invoked in module "Create Python Model",
    # and will not be invoked again in the following modules "Train Model" and "Score Model".
    # The attributes defined in the __init__ method are preserved and usable in the train and predict method.
    def __init__(self):
        # self.model must be assigned
        model = tf.keras.Sequential()

        model.add(tf.keras.layers.Convolution1D(filters=2, kernel_size=1,input_shape=(4,1), name='Conv1'))
        model.add(tf.keras.layers.Flatten())
        model.add(tf.keras.layers.Dense(10, activation='relu', name='Dense1'))
        model.add(tf.keras.layers.Dense(10, activation='relu', name='Dense2'))
        model.add(tf.keras.layers.Dense(3, activation='softmax', name='output'))

        optimizer = tf.keras.optimizers.Adam(lr=0.001)
        model.compile(optimizer, loss='categorical_crossentropy', metrics=['accuracy'])

        self.model = model
        self.feature_column_names = list()

    # Train model
    #   Param<df_train>: a pandas.DataFrame
    #   Param<df_label>: a pandas.Series
    def train(self, df_train, df_label):
        # self.feature_column_names records the column names used for training.
        # It is recommended to set this attribute before training so that the
        # feature columns used in predict and train methods have the same names.
        self.feature_column_names = df_train.columns.tolist()
        encoder = OneHotEncoder(sparse=False)
        df_label = encoder.fit_transform(df_label)
        
        ES = tf.keras.callbacks.EarlyStopping(monitor="val_loss", patience=10)
        
        self.model.fit(df_train, df_label, validation_split=0.1 ,epochs=1000, callbacks=[ES])

    # Predict results
    #   Param<df>: a pandas.DataFrame
    #   Must return a pandas.DataFrame
    def predict(self, df):
        # The feature columns used for prediction MUST have the same names as the ones for training.
        # The name of score column ("Scored Labels" in this case) MUST be different from any other
        # columns in input data.
        pred = self.model.predict(df[self.feature_column_names])
        return pd.DataFrame({'Scored Labels': np.argmax(pred, axis=1)})

我该如何解决这个问题?我在笔记本上尝试了这个模型,并且工作正常,所以没有语法错误,只是Tensorflow的问题。

mjqavswn

mjqavswn1#

我们无法直接在设计器中安装TensorFlow。相反,我们可以在内部调用包含TensorFlow的算法节点。例如,我正在使用DenseNet执行图像分类。请查看以下流程。

下面的屏幕是设计器中流的完整画面。


指令集

相关问题