2022-05-22 11:28阅读: 454评论: 0推荐: 0

Docker 安装tensorflow-jupyter

简介

TensorFlow

TensorFlow是一个基于数据流编程(dataflow programming)的符号数学系统,被广泛应用于各类机器学习(machine learning)算法的编程实现,其前身是谷歌的神经网络算法库DistBelief。Tensorflow拥有多层级结构,可部署于各类服务器、PC终端和网页并支持GPU和TPU高性能数值计算,被广泛应用于谷歌内部的产品开发和各领域的科学研究。

Jupyter

Jupyter Notebook(此前被称为 IPython notebook)是一个交互式笔记本,支持运行 40 多种编程语言。Jupyter Notebook的本质是一个 Web 应用程序,便于创建和共享程序文档,支持实时代码,数学方程,可视化和 markdown。 用途包括:数据清理和转换,数值模拟,统计建模,机器学习等等。

安装

1. 官方镜像

https://hub.docker.com/r/tensorflow/tensorflow/tags

注:本站tensorflow示例代码基于1.x版本【1.15.5】

1. Docker方式安装

docker run -d --name=tensorflow-jupyter -p 8888:8888 -v ~宿主机用户目录/tf:/tf tensorflow/tensorflow:1.15.5-jupyter
# 以root权限进入tensorflow-jupyter
docker exec -it -u root tensorflow-jupyter bash
# 查看容器启动日志
docker logs -f tensorflow-jupyter

1. docker-compose方式安装

version: '3'
services:
  tf-service:
    image: tensorflow/tensorflow:1.15.5-jupyter
    container_name: tensorflow-jupyter
    restart: always
    network_mode: 'host'
    volumes:
      - /etc/localtime:/etc/localtime
      - 宿主机用户目录/tf:/tf

1. docker-compose创建容器

docker-compose up -d tf-service

Jupyter

1. 登录Jupyter

# 通过容器日志查看登录token
docker logs tensorflow-jupyter

[I 03:33:40.842 NotebookApp] Serving notebooks from local directory: /tf
[I 03:33:40.842 NotebookApp] Jupyter Notebook 6.1.6 is running at:
[I 03:33:40.842 NotebookApp] http://e71a7703b0fc:8888/?token=f2add1c7eb1106cbf9e242160f5eb9a4272238bc9c944f8b
[I 03:33:40.842 NotebookApp] or http://127.0.0.1:8888/?token=f2add1c7eb1106cbf9e242160f5eb9a4272238bc9c944f8b

2. 环境配置

1. 进入终端

Web管理界面进入终端或直接进入Docker容器内

# 以root权限进入tensorflow-jupyter
docker exec -it -u root tensorflow-jupyter bash

2. 查看版本

# 查看python版本
python --version
# 查看已安装的库
pip list

3. 代码自动补全

默认juypter可以通过tab键进行代码的提示。默认提示功能可能不起作用,原因可能是jedi版本过高。解决办法是将jedi版本降到0.17版本。安装完需要重启jupyter才能生效。

# 查看jedi当前版本
pip list | grep jedi
# 安装jedi 0.17版本
pip install jedi==0.17

4. Ipyparallel集群

ipyparallel 是 IPython 提供的软件包,基于复杂和强大的架构,提供并行和分布式计算的能力。

# 安装
pip install ipyparallel
# 启用
ipcluster nbextension enable

5. 安装主题

# 安装主题
pip install jupyterthemes
# 更新主题
pip install --upgrade jupyterthemes
# 查看主题列表
jt -l
# 更换主题
jt -t chesterish -f fira -fs 10 -cellw 80% -ofs 10 -dfs 10 -tfs 10 -nfs 10 -T
# 恢复默认主题
jt -r

命令行选项

描述 命令
使用帮助 -h
主题列表 -l
主题安装 -t
代码字体 -f
代码字体大小 -fs(默认值:11 )
Notebook 字体 -nfNotebook
字体大小 -nfs( 默认值:13 )
Text/MD 单元格的字体 -tfText/MD
单元格字体大小 -tfs (默认值:13)
Pandas DF Fontsize -dfs(默认值:9)
输出面积字形大小 -ofs(默认值:8.5 )
Mathjax 字形大小 (%) -mathfs(默认值:100)
介绍页边距 -m(默认值:auto)
单元格的宽度 -cellw ( 默认值:980)
行高 -lineh(默认值:170 )
光标宽度 -cursw(默认值:2)
工具栏可见 -T
名称和标识可见 -N
标志可见 -kl
重置默认主题 -r
强制默认字体 -dfonts

代码示例

matplotlib绘图 https://matplotlib.org/

1. 示例1

# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt

labels = ['G1', 'G2', 'G3', 'G4', 'G5']
men_means = [20, 35, 30, 35, 27]
women_means = [25, 32, 34, 20, 25]
men_std = [2, 3, 4, 1, 2]
women_std = [3, 5, 2, 3, 3]
width = 0.35 # the width of the bars: can also be len(x) sequence
fig, ax = plt.subplots()
ax.bar(labels, men_means, width, yerr=men_std, label='Men')
ax.bar(labels, women_means, width, yerr=women_std, bottom=men_means,label='Women')
ax.set_ylabel('Scores')
ax.set_title('Scores by group and gender')
ax.legend()
plt.show()

2. 示例2

# -*- coding: utf-8 -*-
from matplotlib import cbook
from matplotlib import cm
from matplotlib.colors import LightSource
import matplotlib.pyplot as plt
import numpy as np

dem = cbook.get_sample_data('jacksboro_fault_dem.npz', np_load=True)
z = dem['elevation']
nrows, ncols = z.shape
x = np.linspace(dem['xmin'], dem['xmax'], ncols)
y = np.linspace(dem['ymin'], dem['ymax'], nrows)
x, y = np.meshgrid(x, y)

region = np.s_[5:50, 5:50]
x, y, z = x[region], y[region], z[region]

fig, ax = plt.subplots(subplot_kw=dict(projection='3d'))

ls = LightSource(270, 45)
# To use a custom hillshading mode, override the built-in shading and pass
# in the rgb colors of the shaded surface calculated from "shade".
rgb = ls.shade(z, cmap=cm.gist_earth, vert_exag=0.1, blend_mode='soft')
surf = ax.plot_surface(x, y, z, rstride=1, cstride=1, facecolors=rgb,
                       linewidth=0, antialiased=False, shade=False)
ax.set_xticks([])
ax.set_yticks([])
ax.set_zticks([])
fig.savefig("surface3d_frontpage.png", dpi=25)  # results in 160x120 px image

本文作者:逢生博客

本文链接:https://www.cnblogs.com/wufengsheng/p/16297482.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   逢生博客  阅读(454)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起
  1. 1 晚安 顏人中
  2. 2 出山 花粥 / 王胜娚
  3. 3 我们俩 郭顶
  4. 4 日落大道 梁博
  5. 5 像鱼 王贰浪
  6. 6 把回忆拼好给你 王贰浪
  7. 7 时光背面的我 刘至佳 / 韩瞳
  8. 8 愿你余生漫长 王贰浪
  9. 9 追寻你 王天戈 / 川青
  10. 10 夜空中最亮的星 逃跑计划
  11. 11 孤勇者 陈奕迅
  12. 12 不为谁而作的歌 林俊杰
  13. 13 消愁 毛不易
  14. 14 这一生关于你的风景 隔壁老樊
出山 - 花粥 / 王胜娚
00:00 / 00:00
An audio error has occurred, player will skip forward in 2 seconds.