You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将Spyder中训练完成的带权重Python神经网络部署至Azure服务

Hey Raj, let's break down your deployment pain points and walk through the best solution for your pre-trained neural network model with weights:

Optimal Deployment Approach: Azure ML Managed Online Endpoints

This method is purpose-built for data science models with pre-trained weights, and natively handles legacy Python/TensorFlow environments and multi-file projects—way more reliable than the options you've tried so far.

First, let's address your specific questions quickly:

Quick Answers to Your Initial Questions

  • Azure ML Studio's "Execute Python Script": This component is for one-off batch data processing, not model deployment. It doesn't support persistent weight storage or environment management, so you can safely abandon this approach.
  • Converting .py to .ipynb: You can do this with jupyter nbconvert --to notebook your_script.py, but it's completely unnecessary for deployment. Azure ML works directly with .py files and configuration docs—no need to switch to notebooks.
  • Azure Python Web App: Feasible, but requires manual environment setup, weight management, and web server code (Flask/FastAPI). It's better suited for simple apps, not production-grade ML models.

Step-by-Step Deployment with Azure ML

1. Prepare Your Deployment Files

Gather all your model code, weight files (e.g., .h5, .ckpt folders) in a single directory, then add two critical configuration files:

score.py (Inference Script)

This script loads your weights and handles incoming prediction requests. Customize it to match your model's structure:

import tensorflow as tf
import numpy as np

# Initialize model and load weights once at startup
def init():
    global model
    # Import your model definition from your existing .py file
    from your_model_file import build_your_neural_network
    model = build_your_neural_network()
    model.load_weights("your_trained_weights.h5")  # Path to your weights

# Process prediction requests
def run(raw_data):
    # Parse input data (adjust to match your model's expected format)
    input_data = np.array(raw_data["input_features"])
    # Generate predictions
    predictions = model.predict(input_data)
    # Return results in JSON-friendly format
    return {"predictions": predictions.tolist()}

conda.yml (Environment Configuration)

Define your exact legacy Python/TensorFlow environment so Azure ML replicates it perfectly:

name: tf-legacy-env
channels:
  - conda-forge
  - defaults
dependencies:
  - python=3.8  # Replace with your training Python version
  - tensorflow=2.6  # Replace with your training TensorFlow version
  - numpy=1.21.6
  - pip:
      - azureml-defaults  # Required for Azure ML inference

2. Register Your Model & Environment

You can do this via Python (compatible with your Spyder workflow) or Azure CLI:

Option A: Python Script (Spyder-Friendly)

First install the Azure ML SDK with pip install azureml-core, then run:

from azureml.core import Workspace, Model, Environment
from azureml.core.model import InferenceConfig

# Connect to your Azure ML workspace (download workspace_config.json from Azure portal first)
ws = Workspace.from_config()

# Register your model (includes all code and weights)
registered_model = Model.register(
    workspace=ws,
    model_name="your-neural-network-model",
    model_path="./",  # Path to your directory with code/weights
    description="Pre-trained data science neural network model"
)

# Create your environment from the conda.yml file
env = Environment.from_conda_specification(name="tf-legacy-env", file_path="./conda.yml")

# Configure inference settings
inference_config = InferenceConfig(
    environment=env,
    source_directory="./",
    entry_script="./score.py"
)

Option B: Azure CLI

# Log in to Azure
az login
# Set your Azure ML workspace
az ml workspace set -g your-resource-group -n your-workspace-name
# Register the model
az ml model register --name your-neural-network-model --asset-path ./ --description "Pre-trained NN model"
# Create the environment
az ml environment create --file conda.yml --name tf-legacy-env

3. Deploy to a Managed Online Endpoint

This lets Azure handle server management, scaling, and environment maintenance.

Python Script Deployment

from azureml.core.webservice import AciWebservice, Webservice

# Configure deployment (use ACI for testing, AKS for production)
aci_config = AciWebservice.deploy_configuration(
    cpu_cores=1,
    memory_gb=2,
    auth_enabled=True  # Enable API key authentication
)

# Deploy the service
service = Model.deploy(
    workspace=ws,
    name="nn-model-service",
    models=[registered_model],
    inference_config=inference_config,
    deployment_config=aci_config
)

# Wait for deployment to finish
service.wait_for_deployment(show_output=True)
print(f"Service URL: {service.scoring_uri}")
print(f"API Key: {service.get_keys()[0]}")

CLI Deployment

# Create an endpoint
az ml online-endpoint create --name nn-model-endpoint
# Deploy your model to the endpoint
az ml online-deployment create --endpoint nn-model-endpoint --name blue --model your-neural-network-model --environment tf-legacy-env --code ./ --scoring-script score.py --instance-type Standard_DS2_v2 --instance-count 1

4. Test Your Deployed Service

Send a sample request to verify everything works:

import requests
import json

headers = {
    "Content-Type": "application/json",
    "Authorization": f"Bearer {service.get_keys()[0]}"
}

# Sample input (match your model's expected input shape)
test_data = {
    "input_features": [[0.1, 0.2, 0.3, 0.4, 0.5]]
}

response = requests.post(service.scoring_uri, json=test_data, headers=headers)
print("Prediction Result:", response.json())

Why This Is the Best Option

  • Weight Management: Azure ML automatically packages and hosts your weights alongside your model code—no need to manually upload or reference external storage.
  • Environment Consistency: The conda.yml file ensures Azure replicates your exact legacy Python/TensorFlow environment, eliminating version conflicts.
  • Low Maintenance: Managed endpoints handle scaling, updates, and server health, so you don't have to manage web servers or VMs.
  • Production-Ready: You can easily scale to AKS (Azure Kubernetes Service) for high-traffic scenarios, and enable monitoring/logging with Azure ML tools.

内容的提问来源于stack exchange,提问作者Raj

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 07:49:29