以前在POD里跑起来,没问题的示例代码。

移到jupyter中,多给两个GPU,有时运行就会爆出这个错误:

于是,按网上的意见,暂时加了个使用GPU的指定,

暂时搞定。

如下红色部分。

import timeit
import os
import tensorflow as tf
import numpy as np
from tensorflow.keras.datasets.cifar10 import load_data

os.environ['CUDA_VISIBLE_DEVICES'] = '0,1'

def model():
    x = tf.placeholder(tf.float32, shape=[None, 32, 32, 3])
    y = tf.placeholder(tf.float32, shape=[None, 10])
    rate = tf.placeholder(tf.float32)
    # convolutional layer 1
    conv_1 = tf.layers.conv2d(x, 32, [3, 3], padding='SAME', activation=tf.nn.relu)
    max_pool_1 = tf.layers.max_pooling2d(conv_1, [2, 2], strides=2, padding='SAME')
    drop_1 = tf.layers.dropout(max_pool_1, rate=rate)
    # convolutional layer 2
    conv_2 = tf.layers.conv2d(drop_1, 64, [3, 3], padding="SAME", activation=tf.nn.relu)
    max_pool_2 = tf.layers.max_pooling2d(conv_2, [2, 2], strides=2, padding="SAME")
    drop_2 = tf.layers.dropout(max_pool_2, rate=rate)
    # convolutional layers 3
    conv_3 = tf.layers.conv2d(drop_2, 128, [3, 3], padding="SAME", activation=tf.nn.relu)
    max_pool_3 = tf.layers.max_pooling2d(conv_3, [2, 2], strides=2, padding="SAME")
    drop_3 = tf.layers.dropout(max_pool_3, rate=rate)
    # fully connected layer 1
    flat = tf.reshape(drop_3, shape=[-1, 4 * 4 * 128])
    fc_1 = tf.layers.dense(flat, 80, activation=tf.nn.relu)
    drop_4 = tf.layers.dropout(fc_1 , rate=rate)
    # fully connected layer 2 or the output layers
    fc_2 = tf.layers.dense(drop_4, 10)
    output = tf.nn.relu(fc_2)
    # accuracy
    correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(output, 1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
    # loss
    loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=output, labels=y))
    # optimizer
    optimizer = tf.train.AdamOptimizer(1e-4, beta1=0.9, beta2=0.999, epsilon=1e-8).minimize(loss)
    return x, y, rate, accuracy, loss, optimizer

def one_hot_encoder(y):
    ret = np.zeros(len(y) * 10)
    ret = ret.reshape([-1, 10])
    for i in range(len(y)):
        ret[i][y[i]] = 1
    return (ret)

def train(x_train, y_train, sess, x, y, rate, optimizer, accuracy, loss):
    batch_size = 128
    y_train_cls = one_hot_encoder(y_train)
    start = end = 0
    for i in range(int(len(x_train) / batch_size)):
        if (i + 1) % 100 == 1:
            start = timeit.default_timer()
        batch_x = x_train[i * batch_size:(i + 1) * batch_size]
        batch_y = y_train_cls[i * batch_size:(i + 1) * batch_size]
        _, batch_loss, batch_accuracy = sess.run([optimizer, loss, accuracy], feed_dict={x:batch_x, y:batch_y, rate:0.4})
        if (i + 1) % 100 == 0:
            end = timeit.default_timer()
            print("Time:", end-start, "s the loss is ", batch_loss, " and the accuracy is ", batch_accuracy * 100, "%")

def test(x_test, y_test, sess, x, y, rate, accuracy, loss):
    batch_size = 64
    y_test_cls = one_hot_encoder(y_test)
    global_loss = 0
    global_accuracy = 0
    for t in range(int(len(x_test) / batch_size)):
        batch_x = x_test[t * batch_size : (t + 1) * batch_size]
        batch_y = y_test_cls[t * batch_size : (t + 1) * batch_size]
        batch_loss, batch_accuracy = sess.run([loss, accuracy], feed_dict={x:batch_x, y:batch_y, rate:1})
        global_loss += batch_loss
        global_accuracy += batch_accuracy
    global_loss = global_loss / (len(x_test) / batch_size)
    global_accuracy = global_accuracy / (len(x_test) / batch_size)
    print("In Test Time, loss is ", global_loss, ' and the accuracy is ', global_accuracy)

EPOCH = 100
(x_train, y_train), (x_test, y_test) = load_data()
print("There is ", len(x_train), " training images and ", len(x_test), " images")
x, y, rate, accuracy, loss, optimizer = model()
sess = tf.Session()
sess.run(tf.global_variables_initializer())

for i in range(EPOCH):
    print("Train on epoch ", i ," start")
    train(x_train, y_train, sess, x, y, rate, optimizer, accuracy, loss)
    test(x_train, y_train, sess, x, y, rate, accuracy, loss)

tensorflow运行时错误:服务似乎挂掉了,但是会立刻重启的.的更多相关文章

  1. TensorFlow Serving-TensorFlow 服务

    TensorFlow服务是一个用于服务机器学习模型的开源软件库.它处理机器学习的推断方面,在培训和管理他们的生命周期后采取模型,通过高性能,引用计数的查找表为客户端提供版本化访问. 可以同时提供多个模 ...

  2. linux 编写定时任务,查询服务是否挂掉

    shell 脚本 #!/bin/bash a=`netstat -unltp|grep fdfs|wc -l` echo "$a" if [ "$a" -ne ...

  3. 平时服务正常,突然挂了,怎么重启都起不来,查看日志Insufficient space for shared memory file 内存文件空间不足

    Java HotSpot(TM) 64-Bit Server VM warning: Insufficient space for shared memory file:   /tmp/hsperfd ...

  4. nodejs-Cluster模块

    JavaScript 标准参考教程(alpha) 草稿二:Node.js Cluster模块 GitHub TOP Cluster模块 来自<JavaScript 标准参考教程(alpha)&g ...

  5. 踩坑踩坑之Flask+ uWSGI + Tensorflow的Web服务部署

    一.简介 作为算法开发人员,在算法模块完成后,拟部署Web服务以对外提供服务,从而将算法模型落地应用.本文针对首次基于Flask + uWSGI + Tensorflow + Nginx部署Web服务 ...

  6. Dubbo框架中的应用(两)--服务治理

    Dubbo服务治理了看法 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGlzaGVoZQ==/font/5a6L5L2T/fontsize/400/fi ...

  7. 【深度解析】Google第二代深度学习引擎TensorFlow开源

    作者:王嘉俊 王婉婷 TensorFlow 是 Google 第二代深度学习系统,今天宣布完全开源.TensorFlow 是一种编写机器学习算法的界面,也可以编译执行机器学习算法的代码.使用 Tens ...

  8. linux下监视进程 崩溃挂掉后自动重启的shell脚本

    如何保证服务一直运行?如何保证即使服务挂掉了也能自动重启?在写服务程序时经常会碰到这样的问题.在Linux系统中,强大的shell就可以很灵活的处理这样的事务. 下面的shell通过一个while-d ...

  9. java 服务治理办法

    在大规模服务化之前.应用可能仅仅是通过RMI或Hessian等工具.简单的暴露和引用远程服务,通过配置服务的URL地址进行调用.通过F5等硬件进行负载均衡. (1) 当服务越来越多时.服务URL配置管 ...

随机推荐

  1. Xamarin.Forms移动开发系列2:创建和调试

    摘要 本文将介绍如何通过VS2019创建Xamarin.Forms应用程序,以及如何进行调试. 前言 本文介绍Xamarin.Froms应用程序的创建和调试. 开发环境 1.Visual Studio ...

  2. 洛谷 P1950 长方形_NOI导刊2009提高(2)

    传送门 思路 首先定义\(h\)数组,\(h[i][j]\)表示第\(i\)行第\(j\)列最多可以向上延伸多长(直到一个被用过的格子) 然后使用单调栈算出 \(l_i\)和 \(r_i\) ,分别是 ...

  3. B1020 月饼(25分)

    #include<cstdio> #include<algorithm> #include<iostream> using namespace std; struc ...

  4. Linux性能优化实战学习笔记:第三十六讲

    一.上节总结回顾 上一节,我们回顾了经典的 C10K 和 C1000K 问题.简单回顾一下,C10K 是指如何单机同时处理 1 万个请求(并发连接 1 万)的问题,而 C1000K 则是单机支持处理 ...

  5. Linux性能优化实战学习笔记:第四十四讲

    一.上节回顾 上一节,我们学了网络性能优化的几个思路,我先带你简单复习一下. 在优化网络的性能时,你可以结合 Linux 系统的网络协议栈和网络收发流程,然后从应用程序.套接字.传输层.网络层再到链路 ...

  6. for...in 、for...of 、forEach 的区别

    无论是for…in还是for…of语句都是迭代一些东西.它们之间的主要区别在于它们的迭代方式. 1.for…in 语句以原始插入顺序迭代对象的可枚举属性.2.for…of 语句遍历可迭代对象定义要迭代 ...

  7. (十三)golang--程序流程控制

    1.顺序控制 若没有判断,没有跳转,程序由上至下依次执行 2.分支控制 单分支if 条件表达式 { 执行代码块} 双分支if 条件表达式 { 执行代码块1} else { 执行代码块2} 多分支if ...

  8. Debug 路漫漫-11:Python: TypeError: 'generator' object is not subscriptable

    调试程序,出现以下错误: Python: TypeError: 'generator' object is not subscriptable “在Python中,这种一边循环一边计算的机制,称为生成 ...

  9. No package python-pip available. 解决方法

    问题描述: No package python-pip available. 解决办法: rpm -ivh http://dl.fedoraproject.org/pub/epel/6/i386/ep ...

  10. apache启动错误 AH00072

    错误描述: make_sock: could not bind to address [::]:443 G:\Apache24\bin>httpd.exe -w -n "Apache2 ...