使用Python外部脚本的SQL服务器上的azureml

2024-06-07 18:56:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试从SQL服务器上的Python外部脚本运行Azure Automated ML服务。我在SQL查询中创建了以下过程:

CREATE OR ALTER PROCEDURE [dbo].[PyTrainAutoML]
AS
BEGIN

DECLARE @tenantid NVARCHAR(255) 
DECLARE @appid NVARCHAR(255) 
DECLARE @password NVARCHAR(255)
DECLARE @config_file NVARCHAR(255)
DECLARE @input_query NVARCHAR(255) = 'SELECT *
                                  FROM [automl].[dbo].[energy_sample_training]'

SELECT @tenantid=TenantId, @appid=AppId, @password=[Password], @config_file=ConfigFile
FROM aml_connection

EXEC sp_execute_external_script
@language = N'Python',
@script = N'

from azureml.core import Workspace, Experiment, Run
from azureml.train.automl import AutoMLConfig
import logging
from azureml.core.authentication import ServicePrincipalAuthentication

data_input = input_data

y = data_input.pop(energyProd)
X = data_input

auth = ServicePrincipalAuthentication(tenantid, appid, password)

ws = Workspace.get(
    name = "test",
    subscription_id = "xxx",
    resource_group = "yyy",
auth = auth
)

experiment = Experiment(workspace = ws, name = "energy_AutoML")

automl_config = AutoMLConfig(task = "regression",
                        iteration_timeout_minutes = 10,
                        iterations = 3,
                        primary_metric = "spearman_correlation",
                        n_cross_validations = 5,
                        debug_log = "automl.log",
                        verbosity = logging.INFO,
                        X = X,
                        y = y)

local_run = experiment.submit(automl_config, show_output = True)

best_run, fitted_model = local_run.get_output()

pickled_model = codecs.encode(pickle.dumps(fitted_model), "base64").decode()

returned_model = pd.DataFrame({"best_run": [best_run.id], 
                           "experiment_name": [experiment_name], 
                           "fitted_model": [pickled_model], 
                           "log_file_text": [log_file_text], 
                           "workspace": [ws.name]}, dtype=np.dtype(np.str))
'
, @input_data_1 = @input_query
, @input_data_1_name = N'input_data'
, @output_data_1_name = N'returned_model'
WITH RESULT SETS ((best_run NVARCHAR(250), experiment_name NVARCHAR(100), fitted_model VARCHAR(255), 
log_file_text NVARCHAR(255), workspace NVARCHAR(100)));
END;
GO

但是,当我使用以下命令运行过程时:

EXEC PyTrainAutoML

我得到这个错误:

Msg 39004, Level 16, State 20, Line 7
A 'Python' script error occurred during execution of 'sp_execute_external_script' with HRESULT 0x80004004.
Msg 39019, Level 16, State 2, Line 7
An external script error occurred: 
C:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\PYTHON_SERVICES\lib\site-packages\revoscalepy\RxSerializable.py:257: UserWarning: Pandas doesn't allow columns to be created via a new attribute name - see https://pandas.pydata.org/pandas-docs/stable/indexing.html#attribute-access
  setattr(result, "var_info", var_info)

Error in execution.  Check the output for more information.
Traceback (most recent call last):
  File "<string>", line 5, in <module>
  File "C:\ProgramData\MSSQLSERVER\Temp-PY\Appcontainer1\41BDBB61-0A6A-4D0F-9C4F-898B1680D797\sqlindb_0.py", line 33, in transform
    from azureml.core import Workspace, Experiment, Run
ModuleNotFoundError: No module named 'azureml.core'

SqlSatelliteCall error: Error in execution.  Check the output for more information.
STDOUT message(s) from external script: 
SqlSatelliteCall function failed. Please see the console output for more information.
Traceback (most recent call last):
  File "C:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\PYTHON_SERVICES\lib\site-packages\revoscalepy\computecontext\RxInSqlServer.py", line 605, in rx_sql_satellite_call
    rx_native_call("SqlSatelliteCall", params)
  File "C:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\PYTHON_SERVICES\lib\site-packages\revoscalepy\RxSerializable.py", line 375, in rx_native_call
    ret = px_call(functionname, params)
RuntimeError: revoscalepy function failed.

我曾尝试在sql server上安装AzureML,但似乎还有其他问题。我希望有人能看到东西,我错过了:)


Tags: runnameinfrominputoutputsqldata

热门问题