阿里云AI-深度学习糖尿病预测

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Sat Sep 15 10:54:53 2018
@author: myhaspl
@email:[email protected]
糖尿病预测(多层)
csv格式:怀孕次数、葡萄糖、血压、皮肤厚度,胰岛素,bmi,糖尿病血统函数,年龄,结果
"""

import tensorflow as tf

trainCount=10000
inputNodeCount=8
validateCount=50
sampleCount=200
testCount=10
outputNodeCount=1
ossPath="oss://myhaspl-ai.oss-cn-beijing-internal.aliyuncs.com/"
localPath="./"
dataPath=ossPath

g=tf.Graph()
with g.as_default():

def getWeights(shape,wname):
weights=tf.Variable(tf.truncated_normal(shape,stddev=0.1),name=wname)
return weights

def getBias(shape,bname):
biases=tf.Variable(tf.constant(0.1,shape=shape),name=bname)
return biases

def inferenceInput(x):
layer1=tf.nn.relu(tf.add(tf.matmul(x,w1),b1))
result=tf.add(tf.matmul(layer1,w2),b2)
return result

def inference(x):
yp=inferenceInput(x)
return tf.sigmoid(yp)

def loss():
yp=inferenceInput(x)
return tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(labels=y,logits=yp))

def train(learningRate,trainLoss,trainStep):
trainOp=tf.train.AdamOptimizer(learningRate).minimize(trainLoss,global_step=trainStep)
return trainOp

def evaluate(x):
return tf.cast(inference(x)>0.5,tf.float32)

def accuracy(x,y,count):
yp=evaluate(x)
return tf.reduce_mean(tf.cast(tf.equal(yp,y),tf.float32))

def inputFromFile(fileName,skipLines=1):
#生成文件名队列
fileNameQueue=tf.train.string_input_producer([fileName])
#生成记录键值对
reader=tf.TextLineReader(skip_header_lines=skipLines)
key,value=reader.read(fileNameQueue)
return value

def getTestData(fileName,skipLines=1,n=10):
#生成文件名队列
testFileNameQueue=tf.train.string_input_producer([fileName])
#生成记录键值对
testReader=tf.TextLineReader(skip_header_lines=skipLines)
testKey,testValue=testReader.read(testFileNameQueue)
testRecordDefaults=[[1.],[1.],[1.],[1.],[1.],[1.],[1.],[1.],[1.]]
testDecoded=tf.decode_csv(testValue,record_defaults=testRecordDefaults)
pregnancies,glucose,bloodPressure,skinThickness,insulin,bmi,diabetespedigreefunction,age,outcome=tf.train.shuffle_batch(testDecoded,batch_size=n,capacity=1000,min_after_dequeue=1)
testFeatures=tf.transpose(tf.stack([pregnancies,glucose,bloodPressure,skinThickness,insulin,bmi,diabetespedigreefunction,age]))
testY=tf.transpose([outcome])
return (testFeatures,testY)

def getNextBatch(n,values):
recordDefaults=[[1.],[1.],[1.],[1.],[1.],[1.],[1.],[1.],[1.]]
decoded=tf.decode_csv(values,record_defaults=recordDefaults)
pregnancies,glucose,bloodPressure,skinThickness,insulin,bmi,diabetespedigreefunction,age,outcome=tf.train.shuffle_batch(decoded,batch_size=n,capacity=1000,min_after_dequeue=1)
features=tf.transpose(tf.stack([pregnancies,glucose,bloodPressure,skinThickness,insulin,bmi,diabetespedigreefunction,age]))
y=tf.transpose([outcome])
return (features,y)

with tf.name_scope("inputSample"):
samples=inputFromFile(dataPath+"diabetes.csv",1)
inputDs=getNextBatch(sampleCount,samples)
with tf.name_scope("validateSamples"):
validateInputs=getNextBatch(validateCount,samples)

with tf.name_scope("testSamples"):
testInputs=getTestData(dataPath+"diabetes_test.csv")

with tf.name_scope("inputDatas"):
x=tf.placeholder(dtype=tf.float32,shape=[None,inputNodeCount],name="input_x")
y=tf.placeholder(dtype=tf.float32,shape=[None,outputNodeCount],name="input_y")

with tf.name_scope("Variable"):
w1=getWeights([inputNodeCount,12],"w1")
b1=getBias((),"b1")
w2=getWeights([12,outputNodeCount],"w2")
b2=getBias((),"b2")
trainStep=tf.Variable(0,dtype=tf.int32,name="tcount",trainable=False)

with tf.name_scope("train"):
trainLoss=loss()
trainOp=train(0.005,trainLoss,trainStep)
init=tf.global_variables_initializer()

with tf.Session(graph=g) as sess:
sess.run(init)

coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
logStr=""
while trainStep.eval()<trainCount:

sampleX,sampleY=sess.run(inputDs)
sess.run(trainOp,feed_dict={x:sampleX,y:sampleY})
nowStep=sess.run(trainStep)
if nowStep%500==0:
validate_acc=sess.run(accuracy(sampleX,sampleY,sampleCount))
logStr=logStr+"%d次后=>正确率%g"%(nowStep,validate_acc)+"\n"
print ".",
if nowStep>trainCount:
break

testInputX,testInputY=sess.run(testInputs)
logStr=logStr+"测试样本正确率%g"%sess.run(accuracy(testInputX,testInputY,testCount))
logStr=logStr+str((testInputX,testInputY))
logStr=logStr+str(sess.run(evaluate(testInputX)))
with tf.gfile.GFile(dataPath+"ai_log.txt","wb") as f:
f.write(logStr)
coord.request_stop()
coord.join(threads)

500次后=>正确率0.7
1000次后=>正确率0.8
1500次后=>正确率0.83
2000次后=>正确率0.78
2500次后=>正确率0.775
3000次后=>正确率0.76
3500次后=>正确率0.885
4000次后=>正确率0.85
4500次后=>正确率0.785
5000次后=>正确率0.79
5500次后=>正确率0.795
6000次后=>正确率0.87
6500次后=>正确率0.85
7000次后=>正确率0.79
7500次后=>正确率0.805
8000次后=>正确率0.775
8500次后=>正确率0.87
9000次后=>正确率0.84
9500次后=>正确率0.815
10000次后=>正确率0.805
测试样本正确率1(array([[1.00e+00, 8.90e+01, 6.60e+01, 2.30e+01, 9.40e+01, 2.81e+01,
        1.67e-01, 2.10e+01],
       [8.00e+00, 1.83e+02, 6.40e+01, 0.00e+00, 0.00e+00, 2.33e+01,
        6.72e-01, 3.20e+01],
       [1.00e+00, 1.26e+02, 6.00e+01, 0.00e+00, 0.00e+00, 3.01e+01,
        3.49e-01, 4.70e+01],
       [1.00e+00, 9.30e+01, 7.00e+01, 3.10e+01, 0.00e+00, 3.04e+01,
        3.15e-01, 2.30e+01],
       [8.00e+00, 1.83e+02, 6.40e+01, 0.00e+00, 0.00e+00, 2.33e+01,
        6.72e-01, 3.20e+01],
       [5.00e+00, 1.16e+02, 7.40e+01, 0.00e+00, 0.00e+00, 2.56e+01,
        2.01e-01, 3.00e+01],
       [8.00e+00, 1.83e+02, 6.40e+01, 0.00e+00, 0.00e+00, 2.33e+01,
        6.72e-01, 3.20e+01],
       [1.00e+00, 8.50e+01, 6.60e+01, 2.90e+01, 0.00e+00, 2.66e+01,
        3.51e-01, 3.10e+01],
       [6.00e+00, 1.48e+02, 7.20e+01, 3.50e+01, 0.00e+00, 3.36e+01,
        6.27e-01, 5.00e+01],
       [9.00e+00, 8.90e+01, 6.20e+01, 0.00e+00, 0.00e+00, 2.25e+01,
        1.42e-01, 3.30e+01]], dtype=float32), array([[0.],
       [1.],
       [1.],
       [0.],
       [1.],
       [0.],
       [1.],
       [0.],
       [1.],
       [0.]], dtype=float32))[[0.]
 [1.]
 [1.]
 [0.]
 [1.]
 [0.]
 [1.]
 [0.]
 [1.]
 [0.]]

原文地址:http://blog.51cto.com/13959448/2326090

时间: 2024-08-30 18:12:21

阿里云AI-深度学习糖尿病预测的相关文章

华为云AI-深度学习糖尿病预测

#!/usr/bin/env python2 # -*- coding: utf-8 -*- """ Created on Sat Sep 15 10:54:53 2018 @author: myhaspl @email:[email protected] 糖尿病预测(多层) csv格式:怀孕次数.葡萄糖.血压.皮肤厚度,胰岛素,bmi,糖尿病血统函数,年龄,结果 """ import tensorflow as tf import os tra

机器学习001 deeplearning.ai 深度学习课程 Neural Networks and Deep Learning 第一周总结

Deep Learning Specialization 吴恩达老师最近在coursera上联合deeplearning.ai 推出了有关深度学习的一系列课程,相对于之前的machine learning课程,这次的课程更加实用,作业语言也有matlab改为了python从而更加贴合目前的趋势.在此将对这个系列课程做一个学习笔记. 而这次的Deep Learning Specialization分为五门课程,分别为:Neural Networks and Deep Learning,Improv

斯坦福DAWNBench:华为云ModelArts深度学习训练全球最快

近日,斯坦福大学发布了DAWNBenchmark最新成绩,在图像识别(ResNet50-on-ImageNet,93%以上精度)的总训练时间上,华为云ModelArts排名第一,仅需10分28秒,比第二名提升近44%.成绩证明,华为云ModelArts实现了更低成本.更快速度.更极致的体验. 斯坦福大学DAWNBench是用来衡量端到端的深度学习模型训练和推理性能的国际权威基准测试平台,相应的排行榜反映了当前全球业界深度学习平台技术的领先性.计算时间和成本是构建深度模型的关键资源,DAWNBen

AI - 深度学习入门十四章- 摘要1

原文链接:https://yq.aliyun.com/topic/111 01 - 一入侯门"深"似海,深度学习深几许 什么是"学习"? "如果一个系统,能够通过执行某个过程,就此改进了它的性能,那么这个过程就是学习". 学习的核心目的,就是改善性能. 什么是机器学习? 定义1: 对于计算机系统而言,通过运用数据及某种特定的方法(比如统计的方法或推理的方法),来提升机器系统的性能,就是机器学习. 定义2: 对于某类任务(Task,简称T)和某项性

AI - 深度学习之美十四章-概念摘要(8~14)

原文链接:https://yq.aliyun.com/topic/111 08 反向传播(Back Propagation,简称BP) 算法 在神经网络(甚至深度学习)参数训练中,BP算法占据举足轻重的地位. 实际上BP算法是一个典型的双向算法,但通常强调的是反向传播. 工作流程分两大步: 正向传播输入信号,输出分类信息(对于有监督学习而言,基本上都可归属于分类算法).简单说来,就是把信号通过激活函数的加工,一层一层的向前"蔓延",直到抵达输出层. 反向传播误差信息,调整全网权值.如果

阿里云大学Linux学习路线图(学+测)重磅上线!

推荐:阿里云大学—Linux运维学习路线(点击获取免费课程) 全新“学+测”模式 每阶段包含初.中.高三个难度等级考试,学完即测,找准短板,助您全方位自测掌握程度 课程系统全面 课程体系涵盖从Linux运维到云上运维的5大学习阶段,提供17门免费课程.3项云上技能认证.149个课时,助您全面掌握Linux运维核心技能 Linux运维学习路线图大纲 免费Linux运维学习路线课程:阿里云大学—开发者课堂 原文地址:https://www.cnblogs.com/ciip/p/10881392.ht

Spark on Kubernetes与阿里云的深度整合

最近,笔者尝试将Spark on Kubernetes与阿里云深度整合,设计一个开箱即用的Spark on Kubernetes镜像. 首先通过Terraform在阿里云上一键创建和销毁Kubernetes集群.然后写了一个脚本生成Spark镜像,使其在Kubernetes上运行时可以直接读写阿里云OSS上的数据.最后还写了一个spark-submit脚本,可以让镜像动态地从阿里云OSS上下载需要运行的主程序包(jar). 功能: ?一次编译,多次运行,同时支持共有云.私有云.以及混合云.用户只

阿里云产品深入学习

深入分析RDS与DRDS RDS 关系型分布式数据库服务 DRDS 分布式的关系型分布式数据库服务 ECS 云服务器 LB 负载均衡

当我们谈深度学习时,我们用它落地了什么?

摘要: 近日,阿里云在深度学习方面动作频频,先后发布了OCR证件识别,声纹检测,人脸搜索,视频鉴黄服务以及相似图片搜索功能,下面小编就一一为大家介绍五大功能应用. 现今伴随人工智能在技术上的不断突破,一些领域如计算机视觉,已开始与各个行业进行了深度融合.例如保险行业已通过人脸识别这种新时代的认证方式,来对用户身份信息进行识别与审核.深度学习对人工智能的发展起着至关重要的影响. 近日,阿里云在深度学习方面动作频频,先后发布了OCR证件识别,声纹检测,人脸搜索,视频鉴黄服务以及相似图片搜索功能,下面