岩石物理建模

使用 Amazon SageMaker

在本教程中,您将学习如何使用 Amazon SageMaker 训练和托管机器学习模型,以通过各种测量(例如电缆测井和九口公共井的岩相标签)预测孔隙度。

构建和训练机器学习模型可能令人望而生畏,且生产部署通常既复杂又缓慢。Amazon SageMaker 是一个完全托管的平台,可以帮助开发人员和数据科学家快速而轻松地构建、训练和部署任何规模的机器学习模型。Amazon SageMaker 消除了通常会阻碍开发人员使用机器学习的障碍。
在本教程中,您将在 S3 中存储训练数据,并在 Amazon SageMaker 实例中创建 Jupyter Notebook。接下来,您将使用 XGBoost SageMaker 算法训练模型。最后,您将对已部署的模型端点执行干扰并分析结果。

AWS Free Tier 提供免费的 Amazon SageMaker 套餐,让您可以轻松开始使用 Amazon SageMaker。在注册后的前两个月,您可以享受免费月度套餐,其中包括在笔记本上免费使用 250 小时的 t2.medium 来构建模型,免费使用 50 小时的 m4.xlarge 进行培训,以及免费使用 125 小时的 m4.xlarge 以通过 Amazon SageMaker 托管机器学习模型。

本教程大约需要 20 分钟完成。

注意:本教程中的所有资源均在 us-east-1 弗吉尼亚北部区域创建。

要了解有关 AWS 在石油和天然气领域的更多信息,请访问 aws.amazon.com/oil-and-gas。

使用此教程需要拥有 AWS 账户

Amazon SageMaker 不额外收取费用。您在本教程中创建的资源符合免费套餐条件。 

详细了解免费套餐 >>


第 1 步:创建存储桶并进入 SageMaker 控制台


a.  创建 S3 存储桶或使用现有存储桶。

a.  打开 AWS 管理控制台,您就可以让本分步指南处于打开状态。此屏幕加载后,请输入用户名和密码以便开始操作。然后在搜索栏中键入 SageMaker 并选择 SageMaker 以打开 SageMaker 控制台。 

(单击可放大)


第 2 步:设置 Amazon SageMaker 笔记本实例

在此步骤中,您将设置并配置一个 Amazon SageMaker Notebook 实例。 Amazon SageMaker Notebook 实例是运行 Jupyter Notebook 应用程序的完全托管型机器学习(ML)EC2 计算实例。Amazon SageMaker 管理实例的创建和相关资源。在此实例上,您可以创建一个或多个 Jupyter Notebook。Jupyter Notebook 可用于准备和处理数据、创建训练作业、部署经过训练的模型以及验证已部署的模型。


a.  通过在屏幕右侧的“开始使用”部分中选择“创建笔记本实例”启动 Amazon SageMaker 笔记本。

(单击可放大)


b. 在“笔记本实例设置”部分中,完成以下步骤:1) 为您的笔记本指定一个名称(例如 “ImageIndexing”),2) 选择笔记本实例类型(例如,最小且成本较低的 ml.t2.medium)及 3) 选择“创建新角色”。

(单击可放大)


c. 对于 IAM 角色,创建一个新角色。在“特定 S3 存储桶”中输入步骤 1 中的 S3 存储桶的名称。 此 IAM 角色允许 Amazon SageMaker 实例访问包含训练数据的 S3 存储桶。将 VPC、生命周期配置、加密字段保留为默认值。单击“创建笔记本实例”。

(单击可放大)


d. 等待大约 2-3 分钟,笔记本电脑的状态从“待处理”更改为“服务中”

(单击可放大)


e. 打开笔记本查看 Jupyter Notebook 界面。

(单击可放大)



g.向下滚动角色列表,然后选择您刚刚创建的 SageMaker 角色。选择“附加策略”。

(单击可放大)


h.在以下摘要页面中,确保新策略已添加到规则中。添加策略后,您的笔记本实例将有权访问 Amazon EC2 Container 服务。

(单击可放大)


i.返回 Amazon SageMaker 控制台,检查笔记本的状态是否列为“服务中”。笔记本准备就绪后,选择“打开”。这将在您的实例中打开 Jupyter Web 应用程序。

(单击可放大)


第 3 步:配置 Jupyter Notebook

a. 设置笔记本内核。

内核是内查用户提供的代码的程序。SageMaker 实例上的 Jupyter Notebook 提供多种语言的内核,如图所示。在本教程中,选择“conda_python3”。

(单击可放大)


b. 重命名无标题的 Jupyter Notebook。由于可以在此实例中创建多个笔记本,因此请确保您创建的每个笔记本在实例中都有唯一的名称。

(单击可放大)


d. 在 Jupyter Notebook 中,您将使用两种不同类型的单元格:Code 和 Markdown。

Code 单元格允许您编辑和编写新代码,具有完整的语法突出显示和 tab 键自动完成功能。当 Code 单元格被执行时,它所包含的代码将被发送到与笔记本相关的内核。然后,此计算返回的结果将作为单元格的输出显示在笔记本中。输出不仅限于文本,还可能有许多其他可能的输出形式,包括图形和 HTML 表格。 

您可以以一种文字的方式记录计算过程,使用富文本交替使用描述性文本和代码。这是通过在名为 Markdown 单元格的单元格中使用 Markdown 语言标记文本来实现的。Markdown 语言提供了一种执行此文本标记的简单方法,即指定应强调(斜体)、加粗、以表格列出文本的哪些部分等等。当 Markdown 单元格被执行时,Markdown 代码将转换为相应的格式化富文本。Markdown 允许使用任意 HTML 代码进行格式化。

使用 code 和 markdown 单元格的组合,您将捕捉到应用场景的完整叙述,包括代码和解释代码和结果的注释。这使您可以在单个笔记本中创建可执行代码和详细文档,并与他人共享。

在以下步骤中,您将通过创建和运行 markdown 和 code 单元格的组合来完成 Jupyter Notebook。


第 4 步:在 Jupyter Notebook 中介绍并记录应用场景

a. 输入以下内容作为笔记本的第一个单元格,并将单元格类型更改为“Markdown”

# Petrophysical Modeling with Amazon SageMaker XGBoost algorithm
**Single machine training for regression with Amazon SageMaker XGBoost algorithm**

## Introduction

This notebook demonstrates the use of Amazon SageMaker's implementation of the XGBoost algorithm to train and host a regression model for prediction of porosity from other well log measurements. 

> A Jupyter Notebook is an open-source web application that allows you to create and share documents that contain live code, equations, visualizations and narrative text. - http://jupyter.org/

The dataset is log data from nine wells, with 5 wireline log measurements, two indicator variables and a facies label at half foot intervals. The seven predictor variables are:

Five wireline log curves including: 

- Gamma ray (GR)
- Resistivity logging (ILD_log10)
- Photoelectric effect (PE) - Note, some wells do not have PE.
- Neutron-density porosity difference 
- Average neutron-density porosity (DeltaPHI and PHIND)

Two geologic constraining variables: 

- Nonmarine-marine indicator (NM_M) 
- Relative position (RELPOS)

We will focus on these logs: GR, ILD_log10, DeltaPH, PHIND, where we try to predict PHIND by training using 7 wells, 1 validation well, and one well for blind test. 

The dataset comes from a class exercise from The University of Kansas on Neural Networks and Fuzzy Systems. This exercise is based on a consortium project to use machine learning techniques to create a reservoir model of the largest gas fields in North America, the Hugoton and Panoma Fields. For more info on the origin of the data, see Bohling and Dubois (2003) and Dubois et al. (2007).

(单击可放大)


4b.通过从工具栏中选择“运行”按钮来运行该单元格。

(单击可放大)


4c. 验证相应的输出。

(单击可放大)


第 5 步:获取训练数据并上传到 S3

在以下步骤中,使用步骤 4 中介绍的过程创建“markdown”单元格。笔记本中的单元格将默认为“Code”单元格。)

a. 以“markdown”的形式创建并运行笔记本中的下一个单元格 

## Specify the S3 bucket to upload the training data.

Replace the ‘S3_BUCKET_NAME’ with the S3 bucket you want to use.

b. 创建并运行以下内容

%%time
import os
import boto3
import re
from sagemaker import get_execution_role

role = get_execution_role()
region = boto3.Session().region_name

bucket=’S3_BUCKET_NAME’ # NOTE : put your s3 bucket name here, and create s3 bucket
prefix = 'public-data-example/high-level’
bucket_path = 'https://s3-{}.amazonaws.com/{}'.format(region,bucket)

c.以“markdown”的形式创建并运行以下内容

### Fetching the dataset
Following methods will split the data into train/test/validation datasets and upload files to S3.

d. 使用以下内容创建并运行笔记本中的下一个单元格:

%time

import io
import boto3
import random

# Split the data in FILE_DATA into 3 different files (FILE_TRAIN, FILE_VALIDATION, FILE_TEST)  based on well names (Valid_Well_Name and Blind_Well_Name).      
def data_split(FILE_DATA, FILE_TRAIN, FILE_VALIDATION, FILE_TEST, Valid_Well_Name, Blind_Well_Name, TARGET_VAR):
    data = pd.read_csv(FILE_DATA)
    n = data.shape[0]
    
    # Make the first column the target feature    
    cols = data.columns.tolist()
    target_pos = data.columns.get_loc(TARGET_VAR)
    cols.pop(target_pos)
    cols = [TARGET_VAR] + cols
    data = data.loc[:,cols]
                
    # Split data
    # Use data about the ‘Blind_Well_Name’ well as the test data.
    test_data = data[data['Well Name'] == Blind_Well_Name]
    # From the test data drop the ‘Well Name’ feature as it doesn’t add any value in the training/validation/test process.
    test_data = test_data.drop(['Well Name'], axis=1)
    
    # Use data about the ‘Valid_Well_Name’ well as the validation data.
    valid_data = data[data['Well Name'] == Valid_Well_Name]
 # From the valid data drop the ‘Well Name’ feature as it doesn’t add any value in the     training/validation/test process.
    valid_data = valid_data.drop(['Well Name'], axis=1)

    # Use remaining data is as the training data.
    train_data = data[data['Well Name'] != Blind_Well_Name]
    train_data = train_data[train_data['Well Name'] != Valid_Well_Name]
    # From the training data drop the ‘Well Name’ feature as it doesn’t add any value in the training/validation/test process.
    train_data = train_data.drop(['Well Name'], axis=1)

    train_data.to_csv(FILE_TRAIN, index=False, header=False)
    valid_data.to_csv(FILE_VALIDATION, index=False, header=False)
    test_data.to_csv(FILE_TEST, index=False, header=False)
    
def write_to_s3(fobj, bucket, key):
    return boto3.Session().resource('s3').Bucket(bucket).Object(key).upload_fileobj(fobj)

def upload_to_s3(bucket, channel, filename):
    fobj=open(filename, 'rb')
    key = prefix+'/'+channel+'/'+filename
    url = 's3://{}/{}'.format(bucket, key)
    print('Writing to {}'.format(url))
    write_to_s3(fobj, bucket, key)     
    return(url)

e.以“markdown”的形式创建并运行以下内容

## Data Ingestion

Next, we read the dataset from the existing repository into memory, for preprocessing prior to training. This processing could be done in situ by Amazon Athena, Apache Spark in Amazon EMR, Amazon Redshift, etc., assuming the dataset is present in the appropriate location. Then, the next step would be to transfer the data to S3 for use in training. For small datasets, such as this one, reading into memory isn't onerous, though it would be for larger datasets.

When using the csv option, here is the critical piece of information about the data format:
For CSV training, the algorithm assumes that the target variable is in the first column and that the CSV does not have a header record. For CSV inference, the algorithm assumes that CSV input does not have the label column.

f. 从此 S3 存储桶上传以下训练数据:“facies_vectors.csv”

(单击可放大)


g.运行以下命令从训练数据中移除非数字列和行以及空列和行。

# Remove non-numeric columns
import pandas as pd
data = pd.read_csv('facies_vectors.csv')
# Remove rows with missing values
data.dropna(inplace=True)

data['Well Name'].unique()
data = data.loc[:,['Well Name', 'GR', 'ILD_log10', 'DeltaPHI', 'PHIND']]

# Write file back to disk
data.to_csv('facies_num.csv', index=False)

5h.运行以下命令,根据井名称将数据集拆分为训练、验证和测试数据集。

FILE_DATA = 'facies_num.csv'
TARGET_VAR = 'PHIND'
FILE_TRAIN = 'facies_train.csv'
FILE_VALIDATION = 'facies_validation.csv'
FILE_TEST = 'facies_test.csv'
Valid_Well_Name = 'SHRIMPLIN'
Blind_Well_Name = 'SHANKLE'

data_split(FILE_DATA, FILE_TRAIN, FILE_VALIDATION, FILE_TEST, Valid_Well_Name, Blind_Well_Name, TARGET_VAR)

i.运行以下命令将这些数据集上传到 S3。

# upload the files to the S3 bucket
s3_train_loc = upload_to_s3(bucket = bucket, channel = 'train', filename = FILE_TRAIN)
s3_valid_loc = upload_to_s3(bucket = bucket, channel = 'validation', filename = FILE_VALIDATION)
s3_test_loc = upload_to_s3(bucket = bucket, channel = 'test', filename = FILE_TEST)

您已经使用 Amazon SageMaker 为基于内容的图像索引和检索创建了自定义模型!


现在,您可以针对自己的图像自定义此模型,以尝试推断任何输入图像的相似性。

阅读文档

获取关于如何使用 Amazon SageMaker 的更多信息。

文档 >>

尝试其他教程

分析视频和提取丰富的元数据

立即开始 >>

构建项目

简要了解如何使用 Amazon SageMaker 训练模块,以及如何使用 AWS Lambda 来扩展模块。

阅读博客文章 »


本教程对您是否有帮助?