2 Tensorflow on Kubernetes
TensorFlow 是一个端到端开源机器学习平台。它拥有一个包含各种工具、库和社区资源的全面灵活生态系统,可以让研究人员推动机器学习领域的先进技术的发展,并让开发者轻松地构建和部署由机器学习提供支持的应用。
2.1 在 K8s 中安装 Tensorflow
使用 Webkubectl 进入集群 Terminal, 新建文件 tensorflow.yml 并输入以下内容:
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: tensorflow-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: tensorflow
spec:
selector:
matchLabels:
k8s-app: tensorflow
replicas: 1
template:
metadata:
labels:
k8s-app: tensorflow
spec:
containers:
- name: tensorflow
image: registry.cn-hangzhou.aliyuncs.com/tensorflow-samples/jupyter:1.1.0-devel-gpu
imagePullPolicy: IfNotPresent
env:
- name: PASSWORD
value: mypassw0rd
resources:
limits:
nvidia.com/gpu: 1
volumeMounts:
- mountPath: /usr/local/nvidia
name: nvidia
volumes:
- name: nvidia
persistentVolumeClaim:
claimName: tensorflow-pvc
---
apiVersion: v1
kind: Service
metadata:
name: tensorflow-svc
spec:
type: NodePort
ports:
- name: tensorflow-port
port: 80
targetPort: 8888
selector:
k8s-app: tensorflow
kubectl apply -f tensorflow.yml
persistentvolumeclaim/tensorflow-pvc configured
deployment.apps/tensorflow configured
service/tensorflow-svc configured
kubectl get pod
NAME READY STATUS RESTARTS AGE
tensorflow-79c5f4c48c-94l82 1/1 Running 0 101s
kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
tensorflow-svc NodePort 179.10.100.67 <none> 80:32604/TCP 2m15s
访问 http://NODE_IP:32604 访问 Jupyter ,密码: mypassw0rd
2.2 运行 Tensorflow 深度学习示例
2.2.1 打开 Jupter 终端
2.2.2 使用 PIP 下载 KERAS
pip install keras==2.0.6
2.2.3 示例代码实现
import keras
from tensorflow.python.client import device_lib
# 获取 GPU 信息
#num_gpus = sum([1 for d in local_device_protos if d.device_type == 'GPU'])
#print("GPU : {}".format(num_gpus))
# 下载 MNIST 数据集
mnist = keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
# 将模型的各层堆叠起来,以搭建 keras.Sequential 模型。为训练选择优化器和损失函数:
model = keras.models.Sequential([
keras.layers.Flatten(input_shape=(28, 28)),
keras.layers.Dense(128, activation='relu'),
keras.layers.Dropout(0.2),
keras.layers.Dense(10, activation='softmax')
])
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
# 训练并验证模型
model.fit(x_train, y_train, epochs=5)
model.evaluate(x_test, y_test, verbose=2)
2.2.4 运行深度学习服务
python mnist.py
Epoch 1/5
60000/60000 [==============================] - 9s - loss: 0.2984 - acc: 0.9137
Epoch 2/5
60000/60000 [==============================] - 7s - loss: 0.1422 - acc: 0.9575
Epoch 3/5
60000/60000 [==============================] - 7s - loss: 0.1102 - acc: 0.9671
Epoch 4/5
60000/60000 [==============================] - 7s - loss: 0.0885 - acc: 0.9728
Epoch 5/5
60000/60000 [==============================] - 7s - loss: 0.0758 - acc: 0.9761
[0.074264542010473084, 0.97750000000000004] #这个照片分类器的准确度已经达到 98%
当前内容版权归 KubeOperator 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 KubeOperator .