Sagemaker | Тензор потока | Докер по умолчанию | Как использовать это в ноутбуке
Я хочу использовать Tensorflow в Sagemaker с некоторыми изменениями, чтобы проверить эту функциональность, я импортировал докер Sagemaker 1.9.0 по умолчанию ( https://github.com/aws/sagemaker-tensorflow-container/blob/master/docker/1.9.0/final/py2/Dockerfile.cpu) и успешно загружен в AWS ECR. Теперь я хочу использовать его в своей обучающей модели, но не уверен, как его использовать, не найдя код для вызова модуля докера в коде
это код для загрузки образа докера
from sagemaker import get_execution_role
import boto3
import sagemaker as sage
import boto3
from sagemaker import get_execution_role
role = get_execution_role()
sess = sage.Session()
account = sess.boto_session.client('sts').get_caller_identity()['Account']
region = sess.boto_session.region_name
image = '{}.dkr.ecr.{}.amazonaws.com/preprod-tensorflow'.format(account, region)
Это код, который я хочу обучить данные
import numpy as np
import tensorflow as tf
# Trian dataset
# Height(cm), shoe size (EU), age, weight (kg)
x_train = np.array([[158.0, 38.0, 32.0, 65.0], [185.0, 44.0, 17.0, 70.0], [183.0, 44.0, 50.0, 90.0], [145.0, 36.0, 12.0, 40.0], [165.0, 40.0, 25.0, 65.0]]).astype(np.float32)
# Gender
f = [0, 1]
m = [1, 0]
# Train labels
y_train = np.array([f, m, m, f, f])
# Validation dataset
x_valid = np.array([[175.0, 42.0, 17.0, 65.0], [165.0, 39.0, 32.0, 70.0], [183.0, 43.0, 36.0, 88.0], [174.0, 42.0, 56.0, 77.0], [168.0, 38.0, 28.0, 58.0]]).astype(np.float32)
# Validation labels
y_valid = np.array([f, f, m, f])
# Test data
x_test = np.array([[160.0, 40.0, 45.0, 72.0], [190.0, 45.0, 48.0, 90.0], [165.0, 40.0, 15.0, 60.0], [165.0, 37.0, 20.0, 65.0], [180.0, 43.0, 34.0, 85.0]]).astype(np.float32)
# Test labels
y_test = np.array([f, m, m, f, m])
graph = tf.Graph()
with graph.as_default():
tf_train_dataset = tf.constant(x_train)
tf_train_labels = tf.constant(y_train)
tf_valid_dataset = tf.constant(x_valid)
tf_test_dataset = tf.constant(x_test)
weights = tf.Variable(tf.truncated_normal([4, 2]))
biases = tf.Variable(tf.zeros([2]))
logits = tf.matmul(tf_train_dataset, weights) + biases
loss = tf.reduce_mean(
tf.nn.softmax_cross_entropy_with_logits(labels=tf_train_labels, logits=logits))
# Optimizer.
# We are going to find the minimum of this loss using gradient descent.
optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss)
# Predictions for the training, validation, and test data.
# These are not part of training, but merely here so that we can report
# accuracy figures as we train.
train_prediction = tf.nn.softmax(logits)
valid_prediction = tf.nn.softmax(
tf.matmul(tf_valid_dataset, weights) + biases)
test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases)
num_steps = 5001
def accuracy(predictions, labels):
return (100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1))
/ predictions.shape[0])
with tf.Session(graph=graph) as session:
# This is a one-time operation which ensures the parameters get initialized as
# we described in the graph: random weights for the matrix, zeros for the
# biases.
tf.global_variables_initializer().run()
print('Initialized')
for step in range(num_steps):
# Run the computations. We tell .run() that we want to run the optimizer,
# and get the loss value and the training predictions returned as numpy
# arrays.
_, l, predictions = session.run([optimizer, loss, train_prediction])
if (step % 100 == 0):
print('Loss at step %d: %f' % (step, l))
print('Training accuracy: %.1f%%' % accuracy(predictions, y_train))
# Calling .eval() on valid_prediction is basically like calling run(), but
# just to get that one numpy array. Note that it recomputes all its graph
# dependencies.
print('Validation accuracy: %.1f%%' % accuracy(valid_prediction.eval(), y_valid))
print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), y_test))
Насколько я понимаю, я хочу использовать функцию тензорного изображения Docker для всех тензорных тренировок и т.д., может кто-нибудь, пожалуйста, помогите мне