Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Redis

Feast & Redis Quickstart: สร้าง ML Feature Store ที่ขยายตัวได้ในไม่กี่นาที

บทช่วยสอนนี้ให้ Feast for Redis quickstart แบบทีละขั้นตอน ที่เดินคุณผ่านตัวอย่าง end-to-end ของการใช้ Feast กับ Redis เป็น online feature store สำหรับ machine learning มันพัฒนามาจาก Feast Quickstart tutorial แต่แทนที่จะใช้ online store เริ่มต้น มันใช้ Redis online store สำหรับส่งมอบ real-time predictions ในระยะกว้าง ถ้าคุณไม่คุ้นเคยกับ Feast หรือ Redis วิธีเร็วที่สุดเริ่มต้น Feast ด้วย Redis คือผ่านบทช่วยสอนนี้ โปรดอ้างอิงบทความ Feature Stores and Feast using Redis สำหรับการแนะนำระดับสูง ข้อมูลรายละเอียดเพิ่มเติมเกี่ยวกับ Redis และ Feast รวมถึงทรัพยากรเพิ่มเติม มีให้ที่ท้ายบทช่วยสอน

ในบทช่วยสอนนี้ คุณจะ:

  1. Deploy local feature store ด้วย Parquet file offline store และ Redis online store
  2. สร้าง training dataset โดยใช้ demo time series features จาก Parquet files
  3. Materialize (โหลด) feature values จาก offline store เข้า Redis online store
  4. อ่าน latest features จาก Redis online store สำหรับ inference

คุณสามารถรัน tutorial บน Google Colab หรือ localhost ตามขั้นตอนที่นำทางด้านล่าง

Feast & Redis Quickstart: สร้าง ML Feature Store ที่ขยายตัวได้ในไม่กี่นาที

Feast ใน pocas คำ:

Feast (Feature store) เป็น feature store โอเพนซอร์ส และเป็นส่วนหนึ่งของ Linux Foundation AI & Data Foundation มันสามารถ serve feature data ให้โมเดลจาก online store latency ต่ำ (สำหรับ real-time serving) หรือ offline store (สำหรับ model training หรือ batch serving) มันยังให้ central registry เพื่อให้ machine learning engineers และ data scientists ค้นพบ features ที่เกี่ยวข้องสำหรับ ML use cases ด้านล่างคือสถาปัตยกรรมระดับสูงของ Feast:

Feast เป็น Python library + CLI ที่ optional คุณสามารถติดตั้ง Feast ด้วย pip ได้ ตามที่จะอธิบายเร็วๆ นี้ในบทช่วยสอน

Feast กับ Redis แก้ปัญหาทั่วไปหลายอย่างใน flow นี้:

  1. Training-serving skew และ complex data joins: Feature values บ่อยครั้งอยู่ข้ามหลายตาราง การ join datasets นี้อาจซับซ้อน ช้า และ error-prone
    • Feast join ตารางเหล่านี้ด้วย logic ที่ผ่านการทดสอบแล้ว ที่รับรอง point-in-time correctness ดังนั้น future feature values จะไม่ leak ไปยัง models
  2. Online feature availability ด้วย latency ต่ำและในระยะกว้าง: เวลา inference โมเดลบ่อยครั้งต้องเข้าถึง features ที่ไม่พร้อมใช้งานและต้อง precompute จากแหล่งข้อมูลอื่นในเวลาจริง
    • ด้วยการ deploy Feast กับ Redis คุณรับประกันว่า features ที่จำเป็นพร้อมใช้งานอย่างสม่ำเสมอ และคำนวณใหม่ล่าสุดในเวลา inference ด้วย latency ต่ำและ throughput สูง
  3. Feature reusability และ model versioning: ทีมต่างๆ ในองค์กรบ่อยครั้งไม่สามารถ reuse features ข้ามโปรเจกต์ ทำให้เกิด duplicate feature creation logic โมเดลมี data dependencies ที่ต้อง version เช่นเมื่อรัน A/B tests บน model versions
    • Feast เปิดใช้งานการค้นพบและร่วมมือบน features ที่ใช้ก่อนหน้านี้ และเปิดใช้งาน versioning ของชุด features (ผ่าน feature services)
    • Feast เปิดใช้งาน feature transformation ให้ผู้ใช้ reuse transformation logic ข้าม online/offline use cases และ models

ภาพรวม Feast with Redis Tutorial

ในบทช่วยสอนนี้ เราใช้ feature stores เพื่อสร้าง training data และขับเคลื่อน online model inference สำหรับโมเดลทำนายความพึงพอใจของคนขับ ride-sharing ในสถานการณ์ demo data:

  • เราสำรวจคนขับบางส่วนเพื่อกำหนดว่าพวกเขาพึงพอใจกับประสบการณ์ใช้แอป ride-sharing อย่างไร
  • เราต้องการสร้างการทำนายความพึงพอใจของคนขับสำหรับผู้ใช้คนอื่นๆ เพื่อเราสามารถติดต่อผู้ใช้ที่อาจไม่พึงพอใจได้

ขั้นตอนบทช่วยสอน:

  1. ติดตั้ง Redis และรัน Redis-Server ในพื้นหลัง
  2. ติดตั้ง Feast กับ Redis
  3. สร้าง feature repository และกำหนด Redis เป็น online store
  4. ลงทะเบียน feature definitions และ deploy feature store ของคุณ
  5. สร้าง training data
  6. โหลด features เข้า Redis online store ของคุณ
  7. ดึง feature vectors สำหรับ inference

ขั้นตอนที่ 1: ติดตั้ง Redis และรัน Redis server ในพื้นหลัง

เพื่อติดตั้ง Redis ทำตามทางเลือกใดทางเลือกหนึ่งด้านล่าง:

Ubuntu:

$ sudo snap install redis

Docker:

$ docker run --name redis --rm -p 6379:6379 -d redis

Mac (Homebrew):

$ brew install redis

ข้อมูลเพิ่มเติมเกี่ยวกับวิธีติดตั้ง Redis แบบอื่นๆ หาได้ที่นี่ ข้อมูลการกำหนดค่าเพิ่มเติมหาได้ใน Redis Quick Start guide

ขั้นตอนที่ 2: ติดตั้ง Feast กับ Redis

ติดตั้ง Feast SDK และ CLI ด้วย pip:

$ pip install 'feast[redis]'

ขั้นตอนที่ 3: สร้าง feature repository และกำหนดค่า Redis เป็น online store

Feature repository คือ directory ที่มีการกำหนดค่า feature store และ features ต่างๆ

ขั้นตอนที่ 3a: สร้าง feature repository

วิธีง่ายที่สุดสร้าง feature repository ใหม่คือใช้คำสั่ง feast init สร้าง scaffolding พร้อม demo data เริ่มต้น

$ feast init feature_repo $ cd feature_repo

Output:

Creating a new Feast repository in /Users/nl/dev_fs/feast/feature_repo

มาดู demo repo ที่ได้ มันแบ่งเป็น:

  • feature_store.yaml มี demo setup กำหนดค่า data sources และ online store
  • example.py
    มี demo feature definitions
  • data/ มี raw demo parquet data

ขั้นตอนที่ 3b: กำหนดค่า Redis เป็น online store ในไฟล์ YAML configuration

เพื่อกำหนดค่า Redis เป็น online store เราต้องตั้งค่า type และ connection_string สำหรับ online_store ใน feature_store.yaml ดังนี้:

project: my_project
registry: data/registry.db
provider: local
online_store:
type: redis
connection_string: localhost:6379

Provider กำหนดที่ raw data อยู่ (สำหรับสร้าง training data และ feature values สำหรับ serving) ใน demo นี้ คือ local online_store กำหนดที่จะ materialize (load) feature values ใน online store database (สำหรับ serving)

สังเกตว่าการกำหนดค่าข้างต้นแตกต่างจาก default YAML file ที่ให้สำหรับ tutorial ที่ใช้ default online store แทน:

project: my_project
registry: data/registry.db
provider: local
online_store:
path: data/online_store.db

ดังนั้น ด้วยการเพิ่มสองบรรทัดนี้สำหรับ

online_store
(
type: redis, connection_string: localhost:6379
) ใน YAML file ตามข้างต้น Feast จึงสามารถอ่านและเขียนจาก Redis เป็น online store ได้ Redis Online Store เป็นส่วนหนึ่งของ Feast core code และ Feast รู้จักวิธีใช้ Redis out-of-the-box

ขั้นตอนที่ 3c: ตรวจสอบ demo feature definitions ที่ example.py

มาดู demo feature definitions ที่ example.py (ดูใน terminal รัน

cat
example
.
py 
)

Example.py

ขั้นตอนที่ 3d: ตรวจสอบ raw data

สุดท้าย มาตรวจสอบ raw data Raw data ใน demo นี้จัดเก็บใน local parquet file Dataset จับภาพ hourly stats ของ driver ในแอป ride-sharing

Feast & Redis Quickstart: สร้าง ML Feature Store ที่ขยายตัวได้ในไม่กี่นาที

ขั้นตอนที่ 4: ลงทะเบียน feature definitions และ deploy feature store ของคุณ

ตอนนี้เรารัน

feast apply
เพื่อลงทะเบียน feature views และ entities ที่กำหนดใน example.py คำสั่ง apply สแกน Python files ใน directory ปัจจุบันสำหรับ feature view/entity definitions ลงทะเบียน objects และ deploy infrastructure ในตัวอย่างนี้ มันอ่าน example.py (แสดงด้านบน) และตั้งค่า Redis online store

$ feast apply

Output:

Registered entity driver_id
Registered feature view driver_hourly_stats
Deploying infrastructure for driver_hourly_stats

ขั้นตอนที่ 5: สร้าง training data

เพื่อ train โมเดล เราต้องการ features และ labels บ่อยครั้ง label data จัดเก็บแยก (เช่นคุณมีตารางเก็บผลสำรวจผู้ใช้ และอีกชุดตารางกับ feature values)

ผู้ใช้สามารถ query ตาราง labels นั้นกับ timestamps และส่งเข้า Feast เป็น entity dataframe สำหรับ training data generation ในหลายกรณี Feast จะ join ตารางที่เกี่ยวข้องอย่างฉลาดเพื่อสร้าง feature vectors ที่เกี่ยวข้อง

  • สังเกตว่าเรารวม timestamps เพราะเราต้องการ features สำหรับ driver เดียวกันที่ timestamps ต่างๆ เพื่อใช้ในโมเดล

Python

(คัดลอกโค้ดด้านล่างลงไฟล์ gen_train_data.py แล้วรัน):

Output:

----- Feature schema -----

<class 'pandas.core.frame.DataFrame'>
Int64Index: 3 entries, 0 to 2
Data columns (total 6 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 event_timestamp 3 non-null datetime64[ns, UTC]
1 driver_id 3 non-null int64
2 label_driver_reported_satisfaction 3 non-null int64
3 conv_rate 3 non-null float32
4 acc_rate 3 non-null float32
5 avg_daily_trips 3 non-null int32
dtypes: datetime64[ns, UTC](1), float32(2), int32(1), int64(2)
memory usage: 132.0 bytes
None

----- Example features -----

event_timestamp driver_id ... acc_rate avg_daily_trips
0 2021-08-23 15:12:55.489091+00:00 1003 ... 0.120588 938
1 2021-08-23 15:49:55.489089+00:00 1002 ... 0.504881 635
2 2021-08-23 16:14:55.489075+00:00 1001 ... 0.138416 606

[3 rows x 6 columns]

ขั้นตอนที่ 6: โหลด features เข้า Redis online store ของคุณ

เราจะโหลดหรือ materialize feature data เข้า Redis online store ตอนนี้ เพื่อเราสามารถ serve latest features ให้โมเดลสำหรับ online prediction คำสั่ง

materialize
ให้ผู้ใช้ materialize features ช่วงเวลาเฉพาะในอดีตเข้า online store มันจะ query batch sources สำหรับ feature views ทั้งหมดช่วงเวลาที่ให้มา และโหลด latest feature values เข้า online store ที่กำหนดค่า
materialize
–
incremental
จะ ingest เฉพาะข้อมูลใหม่ที่มาถึงใน offline store ตั้งแต่ materialize call ครั้งล่าสุด

$ CURRENT_TIME=$(date -u +"%Y-%m-%dT%H:%M:%S")
$ feast materialize-incremental $CURRENT_TIME

Output:

Materializing 1 feature views to 2021-08-23 16:25:46+00:00 into the redis online store.

driver_hourly_stats from 2021-08-22 16:25:47+00:00 to 2021-08-23 16:25:46+00:00:
100%|████████████████████████████████████████████| 5/5 [00:00<00:00, 592.05it/s]

ขั้นตอนที่ 7: ดึง feature vectors สำหรับ inference

เวลา inference เราต้องอ่าน latest feature values รวดเร็วสำหรับ drivers ต่างๆ (ซึ่งอาจมีอยู่แค่ใน batch sources) จาก Redis online feature store ใช้

get_online_features()
Feature vectors เหล่านี้จึงสามารถส่งให้โมเดล

Python

(คัดลอกโค้ดด้านล่างลงไฟล์ get_feature_vectors.py แล้วรัน):

Output

{
'acc_rate': [0.5732735991477966, 0.7828438878059387],
'avg_daily_trips': [33, 984],
'conv_rate': [0.15498852729797363, 0.6263588070869446],
'driver_id': [1004, 1005]
}

ยินดีด้วย! คุณถึงสิ้นสุดบทช่วยสอนแล้ว เพื่อปิด Redis server ที่รันในพื้นหลัง ใช้คำสั่ง

redis-cli shutdown

สรุปบทช่วยสอน:

ในบทช่วยสอนนี้ คุณได้ deploy local feature store ด้วย Parquet file offline store และ Redis online store คุณสร้าง training dataset ใช้ time series features จาก Parquet files แล้วคุณ materialized feature values จาก offline store เข้า Redis online store สุดท้ายคุณอ่าน latest features จาก Redis online store สำหรับ inference ด้วย Redis เป็น online store คุณอ่าน latest feature ได้เร็วมากสำหรับ real-time ML use cases ด้วย latency ต่ำและ throughput สูงในระยะกว้าง

ต่อไปคืออะไร?

  • อ่าน Feast Concepts page เข้าใจ Feast data model และอ่าน Feast Architecture page
  • อ่านคู่มือการกำหนดค่าเต็มสำหรับ Feast กับ Redis และ data model ที่ใช้จัดเก็บ feature values ใน Redis
  • Case studies – เรียนรู้จากเพื่อนร่วมงาน: เรียนรู้ว่าบริษัทต่างๆ ใช้ Feature Stores กับ Redis เป็น online store (Wix, Swiggy, Comcast, Zomato, AT&T, DoorDash, iFood) และเฉพาะเจาะจงว่าพวกเขาใช้ Feast กับ Redis สำหรับ online store อย่างไร (Gojek, Udaan, Robinhood)
  • อ่านเกี่ยวกับ Azure Managed Feature Store กับ Feast และ Redis และทำตาม Getting started with Feast on Azure tutorial รวมถึง Feast tutorials อื่นๆ
  • คุณสามารถหาข้อมูลเพิ่มเติมเกี่ยวกับ Feast หรือ Redis ในหน้าแนะนำผลิตภัณฑ์ทั่วไปของ Feast และ Redis ตามลำดับ

ร่วมผู้ใช้และผู้มีส่วนร่วม Feast ใน Slack และเป็นส่วนหนึ่งของชุมชน!