首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >SQL基础用法(实例一)

SQL基础用法(实例一)

作者头像
用户1112962
发布于 2018-07-03 07:40:05
发布于 2018-07-03 07:40:05
99200
代码可运行
举报
运行总次数:0
代码可运行
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
  1 /*
  2 
  3 
  4 2006年10月01日
  5 
  6 SQL Server 数据库的基本操作
  7 (1) 数据库的创建
  8 (2) 数据表的创建以及相关约束的指定(含临时表)
  9 (3) 数据的添/删/改
 10 (4) 数据的查询
 11 
 12 */
 13 
 14 (0)创建数据库
 15 -- 指定数据库名称
 16 -- (注:如果数据库名中包含空格可以使用[]将其标示)
 17 create database [Super WC]
 18 -- 关于数据文件的定义
 19 on
 20 (
 21 name = Super_WC_Data,                -- 逻辑名
 22 filename = 'C:\Super_WC_Data.MDF',        -- 物理路径以及物理名
 23 size = 2MB,                    -- 初始大小
 24 maxsize = 4MB,                    -- 最大限制
 25 filegrowth = 1MB                                            -- 增长大小
 26 )
 27 -- 关于日志文件的定义
 28 log on
 29 (
 30 name = Super_WC_Log,
 31 filename = 'C:\Super_WC_Log.LDF',
 32 size = 3MB,
 33 maxsize = 7MB,
 34 filegrowth = 20%                                            -- 增长比例
 35 )
 36 
 37 -- 附加数据库
 38 execute sp_attach_db '[Super WC]', 'C:\Super_WC_Data.MDF','C:\Super_WC_Log.LDF'
 39 -- 分离数据库
 40 execute sp_detach_db '[Super WC]'
 41 -- 复制数据库
 42 execute master.dbo.xp_cmdshell 'copy C:\Super_WC_Data.MDF D:\Super_WC_Data.MDF'
 43 execute master.dbo.xp_cmdshell 'copy C:\Super_WC_Log.LDF D:\Super_WC_Log.LDF'
 44 
 45 
 46 (1)创建数据表
 47 
 48 创建一个数据表:学生(students)
 49 结构如下:
 50 字段       类型        是否允许为空     约束           备注
 51 no         char(4)     No               主键           学号
 52 
 53 name       nvarchar(8) No               唯一           姓名
 54 
 55 birthday   datetime    No               检查(至少18年前) 生日
 56 
 57 age        tinyint     No               缺省(默认等于当前时间减去生日) 年龄
 58 
 59 sex        nchar(1)    No               缺省(默认'女')                                 性别
 60 
 61 phone      char(11)    Yes              检查(要么没有,要么长度等于11) 电话
 62 
 63 address    nvarchar(24)No                                地址
 64 
 65 没有特别约束的情况:
 66 create table student
 67 (
 68 no        char(4)        not null,
 69 name      nvarchar(8)        not null,
 70 birthday  datetime        not null,
 71 phone     char(11)        null,
 72 address   nvarchar(24)        null
 73 )
 74 
 75 注意:没有特别约束的情况下,创建数据表可以参考“企业管理器”中“设计表”的操作格式!
 76 
 77 包含约束的情况:
 78 create table students
 79 (
 80 no        char(4)         primary key,
 81 name      nvarchar(8)         unique,
 82 birthday  datetime         check(datediff(year, birthday, getdate()) >= 18),
 83 age       as datediff(year, birthday, getdate()),
 84 sex      nchar(1)        default('女') check(sex = '女' or sex = '男'),
 85 phone     char(11)         check((phone is null) or (len(phone) = 11)),
 86 address   nvarchar(24)
 87 )
 88 
 89 
 90 create table scores
 91 (
 92 no                char(4)                foreign key references students(no),
 93 chinese        numeric(4,1)    check(chinese >= 0 and chinese <= 100),
 94 english        numeric(4,1)    check(english >= 0 and english <= 100)     
 95 )
 96 
 97 以上答案只是最简单的描述形式!
 98 
 99 比较规范的写法是
100 先用create table声明数据表的结构, 
101 
102 CREATE TABLE students
103 (
104 no       char(4),
105 name     nvarchar(8),
106 birthday datetime,
107 age      as DATEDIFF(year, birthday, getdate()),
108 sex             nchar(1),
109 phone    char(11),
110 address  nvarchar(24)
111 )
112 
113 然后再ALTER TABLE  ADD CONSTRAINT 分别指定每个字段的约束:
114 每个约束都有一个独特的名称,其中,命名规范采用以下格式:
115 约束类型的简写_表名_字段名
116 pk_students_no
117 
118 ALTER TABLE students
119 ADD CONSTRAINT pk_students_no PRIMARY KEY (no)
120 
121 ALTER TABLE students
122 ADD CONSTRAINT uq_students_name UNIQUE (name)
123 
124 ALTER TABLE students
125 ADD CONSTRAINT ck_students_birthday CHECK (datediff(year,[birthday],getdate()) >= 18)
126 
127 ALTER TABLE students
128 ADD CONSTRAINT df_students_sex default ('女') for sex
129 
130 ALTER TABLE students
131 ADD CONSTRAINT ck_students_sex CHECK ([sex] = '男' or [sex] = '女')
132 
133 ALTER TABLE students
134 ADD CONSTRAINT ck_students_phone CHECK ([phone] is null or len([phone]) = 11)
135 
136 相对应的对约束进行删除,则是通过DROP CONSTRAINT子句来完成:
137 ALTER TABLE students
138 DROP CONSTRAINT pk_students_no
139 
140 ALTER TABLE students
141 DROP CONSTRAINT uq_students_name
142 
143 注意:
144 约束只有添加与删除操作,没有修改操作!
145 
146 
147 注意:
148 其中,age(年龄)采用了“计算列”的表示方法!
149 “计算列”的定义:
150 在表中某个字段的值源于某一表达式的值(某一函数的运算结果或是其他字段的运算结果)!
151 比如:
152 某学生的成绩表结构如下:
153 数学
154 语文
155 体育
156 总分
157 
158 创建命令如下:
159 create table scores
160 (
161 math      numeric(3, 1),
162 chinese  numeric(3, 1),
163 sport     numeric(3, 1),
164 total      as math + Chinese + sport
165 )
166 
167 insert into scores values (80, 69, 95)
168 
169 total 部分的值会自动计算,为 244
170 
171 -- 创建临时表
172 -- 临时表将会存储在TempDB的临时数据库中
173 -- 当用户断开连接后,就会自动删除
174 -- 语法:在表名前加上#
175 create table #tt
176 (
177 a int,
178 b int
179 )
180 
181 insert into #tt values (1,1)
182 insert into #tt values (2,2)
183 
184 select * from #tt
185 
186 
187 (2)数据操作(添加/删除/修改)
188 
189 添加操作(insert)的语法格式:
190 Insert [into] 数据表 (字段) values (数据)
191 
192 -- 添加记录(一般情况)
193 insert into students 
194 (no,name,birthday,sex,phone,address) 
195 values 
196 ('0001', 'AHuang', '2000-01-01', '男', '13307331100', '株洲')
197 
198 (注意: into 可以省略 )
199 
200 
201 -- 添加记录(如果是给所有字段添加数据,可以省略字段标示)
202 insert into students 
203 values 
204 ('0002', 'ANing', '2008-08-08', '女', '13307330011', '北京')
205 
206 
207 -- 添加记录(如果是给具有默认约束的字段添加数据,想利用默认约束,可以利用default)
208 insert into students 
209 values 
210 ('0002', 'ANing', '2008-08-08', default, '13307330011', '北京')
211 
212 
213 删除操作(delete)的语法格式:
214 Delete [from] 数据表 where 条件
215 
216 -- 删除记录(删除所有)
217 delete from students 
218 
219 (注意: from 可以省略,即可以: delete students )
220 
221 -- 删除记录(删除特定记录,可以通过 where 条件来过滤,比如:学号'0001'的学生记录)
222 delete from students where no = '0001'
223 
224 
225 修改操作(update)的语法格式:
226 update 数据表 set 字段 = 新值 where 条件
227 
228 -- 修改记录(修改所有)
229 update students set 性别 = '女'
230 
231 -- 修改记录(修改特定记录,可以通过 where 条件来过滤,比如:学号'0001'的学生记录)
232 update students set 性别 = '男' where no = '0001'
233 
234 
235 
236 (3)数据查询
237 
238 查询操作(select)的语法格式:
239 select 字段 from 数据表 where 条件 order by 字段
240 
241 -- 查询记录(查询所有行与所有列,指定数据表所有字段)
242 select no, name, birthday, sex, phone, address from students 
243 
244 
245 -- 查询记录(查询所有行与所有列,除了指定数据表所有字段,还可以通过 * 来指代所有字段)
246 select * from students 
247 
248 -- 查询记录(查询所有行与特定列,指定数据表特定字段)
249 select no, name, phone, address from students 
250 
251 -- 查询记录(给字段指定别名)
252 select no as '学号', name as '姓名', phone as '电话', address as '地址' from students 
253 
254 -- 查询记录(合并字段并指定别名)
255 select no as '学号', name as '姓名', address + ' ' + phone as '地址 电话' from students 
256 
257 (注意:合并字段的前提是字段类型必须一致,否则需要通过类型转换函数convert来实现!)
258 
259 -- 类型转换函数 convert 语法
260 convert(目标类型, 数据)
261 
262 convert(varchar(12), 1234)
263 
264 
265 (4)数据查询的高级应用(利用PUBS数据库作为演示数据)
266 
267 -- 根据价格对书籍表记录排序
268 select * from titles order by price
269 
270 (注意:默认数据排序采用升序:由低到高或是有小到大,可以通过设定关键字来调整)
271 
272 -- 根据价格对书籍表记录排序(降序)
273 select * from titles order by price desc
274 
275 -- 根据价格对书籍表记录排序(升序)
276 select * from titles order by price asc
277 
278 -- 找出书籍表中最高与最低的价格
279 select max(price), min(price) from titles 
280 
281 -- 统计书籍表中书籍总数以及总价格和平均价格
282 select count(title_id), sum(price), avg(price) from titles 
283 
284 -- 找出书籍表中最贵3本书
285 select top 3 title_id, price from titles order by price desc
286 
287 select * from titles
288 select * from jobs
289 -- 
290 
291 -- 统计书籍表中每种类型的书籍总量
292 select type as '书籍类型', count(title_id) '书籍数量' 
293 from titles group by type
294 
295 select type, title_id from titles order by type
296 
297 -- 统计书籍表中每种类型的书籍的预付款(advance)的总和
298 select type as '书籍类型', sum(advance) '预付款总和' 
299 from titles group by type
300 
301 -- 列出所有书籍类型
302 select type as '书籍类型' from titles
303 select distinct type as '书籍类型' from titles
304 select distinct type as '书籍类型', title_id from titles
305 
306 -- 列出所有作者所在州
307 select distinct state as '州' from authors
308 select distinct state as '州', city as '城市' from authors where  state = 'CA'
309 select state as '州', city as '城市' from authors where state = 'CA'
310 
311 (: distinct 列出指定字段的值同时剔出所有重复的!)
312 
313 
314 -- 根据给定的作者姓名列出其所著书籍名称
315 select * from authors
316 select * from titles
317 
318 select a.au_lname as '名', a.au_fname as '姓', t.title as '书籍名称'
319 from authors as a join titleauthor as ta on a.au_id = ta.au_id
320     join titles as t on ta.title_id = t.title_id
321 where a.au_lname = 'Green' and a.au_fname = 'Marjorie'
322 
323 
324 -- 根据给定的出版社名称列出其所有的出版书籍名称
325 select * from publishers
326 
327 select p.pub_name as '出版社', t.title as '书籍名称'
328 from publishers as p join titles as t on p.pub_id = t.pub_id
329 where pub_name = 'New Moon Books'
330 
331 select pub_name as '出版社', title as '书籍名称'
332 from publishers as p join titles as t on p.pub_id = t.pub_id
333 where pub_name = 'New Moon Books'
334 
335 
336 -- 在销售表中挑选1993年度的订单
337 select ord_num as '订单编号', title_id as '书籍编号', ord_date as '订购日期'  
338 from sales 
339 where ord_date between '1993-01-01' and '1993-12-31'
340 
341 (注意:between and 之间的值必须满足"开始点"<="结束点")
342 select ord_num as '订单编号', title_id as '书籍编号', ord_date as '订购日期'  
343 from sales 
344 where ord_date between '1993-12-31' and '1993-1-1'
345 
346 
347 -- 在销售表中统计每本书的订单数量
348 select title_id as '书籍编号', count(ord_num) as '订单总数'  from sales group by title_id
349 
350 
351 -- 在销售表中统计每本书的总订购量
352 select title_id as '书籍编号', sum(qty) as '总定购量'  from sales group by title_id
353 
354 -- 在销售表中统计每个商店的总订购量
355 select stor_id as '商店编号', sum(qty) as '总定购量'  from sales group by stor_id
356 
357 -- 在销售表中查询每个商店定购了那几本书以及每本书相应的订购量
358 select stor_id as '商店编号', title_id as '书籍编号', sum(qty) as '总定购量'  from sales group by stor_id, title_id order by stor_id
359 
360 
361 
362 -- 查询指定出版社出版的书
363 select p.pub_name as '出版社', t.title as '书籍'
364 from publishers as p join titles as t on p.pub_id = t.pub_id
365 where pub_name = 'New Moon Books'
366 
367 -- 查询指定出版社出版的书籍的总数目
368 select p.pub_name as '出版社', count(t.title) as '书籍数目'
369 from publishers as p join titles as t on p.pub_id = t.pub_id
370 group by p.pub_name
371 
372 -- 统计每个作者的版权税总和
373 select au_id as '作者编号', sum(royaltyper)  from titleauthor group by au_id
374 
375 
376 
377 select * from authors
378 select * from titles
379 select * from publishers
380 
381 select * from publishers where  = 'New Moon Books'
382 select * from titles where pub_id = '0736'
383 
384 -- 子查询
385 -- 根据出版社的名称查询其出版的书籍
386 -- 首先,根据出版社的名称在publisher中找到相应的出版社编号
387 -- 然后,在根据出版社编号在titles中找到相应的书籍信息
388 select * from titles 
389 where pub_id = 
390     (select pub_id from publishers 
391      where pub_name = 'New Moon Books')
392 
393 
394 select title_id, type, price from titles
395 
396 select title_id, type, price 
397 from titles 
398 order by price desc
399 
400 
401 select top 4 title_id, type, price 
402 from titles 
403 order by price desc
404 
405 
406 -- 子查询
407 -- 找寻最贵的书籍
408 -- 先通过聚合函数获取最高价格
409 -- 然后,将其作为查询条件,找出相应的书籍
410 select title_id, type, price from titles
411 where price = (select max(price) from titles)
412 
413 select au_id,au_lname from authors where au_lname = 'Green'
414 
415 -- 子查询
416 -- 根据作者的名查找其编写的书籍
417 -- 先通过子查询获取作者编号
418 -- 然后,将其作为查询条件,找出相应的书籍编号
419 -- 最后,在利用所得到的书籍编号来得到书籍信息
420 select au_id, title_id from titleauthor
421 where au_id = 
422     (select au_id from authors where au_lname = 'Green')
423 
424 select * from titles
425 where title_id in 
426     (
427     select title_id from titleauthor
428         where au_id = 
429         (select au_id from authors where au_lname = 'Green')
430     )
431 
432 
433 
434 -- 打印输出
435 -- Print
436 print 'Hello world'
437 
438 -- 获取系统时间
439 print getdate()
440 
441 -- 获取3天前的时间
442 print dateadd(day, -3 , getdate())
443 -- 获取3天后的时间
444 print dateadd(day, 3 , getdate())
445 -- 获取3年前的时间
446 print dateadd(year, -3 , getdate())
447 -- 获取3年后的时间
448 print dateadd(year, 3 , getdate())
449 
450 -- 获取3月后的时间
451 print dateadd(month, 3 , getdate())
452 -- 获取9小时后的时间
453 print dateadd(hour, 9 , getdate())
454 -- 获取9分钟后的时间
455 print dateadd(minute, 9 , getdate())
456 
457 -- 获取指定时间之间相隔多少年
458 print datediff(year, '2005-01-01', '2008-01-01')
459 -- 获取指定时间之间相隔多少月
460 print datediff(month, '2005-01-01', '2008-01-01')
461 -- 获取指定时间之间相隔多少天
462 print datediff(day, '2005-01-01', '2008-01-01')
463 
464 -- 获取给定字符串的长度
465 print len('abcdef')
466 -- 字符串合并
467 print 'abc' + 'def'
468 
469 print 'abcder'
470 
471 print 'abc' + '456'
472 print 'abc' + 456
473 -- 类型转换
474 print 'abc' + convert(varchar(10), 456)
475 
476 print '123' + '456'
477 print '123' + 456
478 print 123 + '456'
479 
480 
481 -- 利用系统函数作为默认值约束
482 drop table ttt
483 
484 create table ttt
485 (
486 stu_name    varchar(12),
487 stu_birthday    datetime default (getdate())
488 )
489 
490 alter table ttt
491 add constraint df_ttt_stu_birthday default  (getdate()) for stu_birthday
492 
493 insert into ttt values ('ANiu', '2005-04-01')
494 insert into ttt values ('ANiu', getdate())
495 
496 insert into ttt values ('AZhu', default)
497 
498 sp_help ttt
499 
500 select * from ttt
501 
502 -- 计算列
503 create table scores
504 (
505 chinese int,
506 english int,
507 total int
508 )
509 
510 insert into scores values (80, 90, 170)
511 
512 select * from scores
513 
514 drop table scores
515 
516 create table scores
517 (
518 chinese int,
519 english int,
520 total as chinese + english    -- 计算列
521 )
522 
523 insert into scores values (80, 90)
524 
525 -- 故意添加,结果出错
526 insert into scores values (80, 90, 250)
527 
528 select * from scores
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017-03-14 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
tensorflow学习笔记(四十一):control dependencies
本文介绍了tensorflow控制依赖的两种方式,分别是tf.control_dependencies和tf.assign。同时,本文还介绍了两种不适用于tf.control_dependencies的情况,分别是使用变量初始值作为控制依赖以及在一个Session中同时使用多个控制依赖。
ke1th
2018/01/02
2.1K0
卷积神经网络处理图像识别(三)
下面是测试Batch的总Loss和验证集上的准确率的收敛趋势图。由于我的电脑性能不好,所以我大幅度削减了待训练参数个数。尽管如此,2000轮训练之后,在验证集上5000个图片的预测正确率已达98.3%。如若不削减参数,准确率可达99.4%。
用户6021899
2019/11/25
9060
卷积神经网络处理图像识别(三)
关于tf.train.ExponentialMovingAverage使用的详细解析
tf.train.ExponentialMovingAverage是指数加权平均的求法,具体的公式是 total=a*total+(1-a)*next,
狼啸风云
2020/09/24
1.5K0
TensorFlow指南(三)——深度神经网络(初级)
由于本系列博文主要专注于Tensorflow本身,所以还是老样子不会过多讲解神经网络的理论知识。 可以参阅这篇博文来先理解下神经网络:http://blog.csdn.net/u011239443/article/details/76680704
小爷毛毛_卓寿杰
2019/02/13
4420
Tensortflow学习笔记
w=tf.Variable(tf.random_normal(2,3,stddev=2, mean=0, seed=1))
freesan44
2021/10/12
5220
TensorFlow-手写数字识别(三)
本篇文章在上篇TensorFlow-手写数字识别(二)的基础上,将全连接网络改为LeNet-5卷积神经网络,实现手写数字识别。
xxpcb
2020/08/04
1K0
Tensorflow滑动平均模型
移动平均法是用一组最近的实际数据值来预测未来一期或几期内公司产品的需求量、公司产能等的一种常用方法。移动平均法适用于即期预测。当产品需求既不快速增长也不快速下降,且不存在季节性因素时,移动平均法能有效地消除预测中的随机波动,是非常有用的。移动平均法根据预测时使用的各元素的权重不同
演化计算与人工智能
2020/08/14
1.5K0
Tensorflow滑动平均模型
tensorflow实现手写体数字识别
之前在人工智能课上自己手动搭建过一个BP神经网络实现MNIST数据集的手写体数字识别,使用的是c++,最终准确率的上限在95%至96%左右(毕竟水平有限)。这次不一样了,使用tensorflow进行实验,准确率确实提高了不少。可能有人会觉得tensorflow有点过时,现在的大企业不怎么用tensorflow了,但我觉得,对于初学者来说,tensorflow还是不错的选择。
luxuantao
2021/02/24
1.2K0
持久化的基于 L2 正则化和平均滑动模型的 MNIST 手写数字识别模型
[1]Tensorflow实战Google深度学习框架: https://github.com/caicloud/tensorflow-tutorial/tree/master/Deep_Learning_with_TensorFlow/1.4.0
演化计算与人工智能
2020/08/14
4480
tensorflow的模型持久化
tensorflow提供了一个非常简单的API来保存和还原一个神经网络模型。这个API就是tf.train.Saver类。以下代码给出了保存tensorflow计算图的方法。
狼啸风云
2019/03/06
2K0
tf24: GANs—生成明星脸
本文介绍了如何使用TensorFlow实现生成对抗网络(GANs),用于生成明星脸。首先,介绍了TensorFlow的基本概念,然后详细阐述了如何搭建一个GANs模型。接着,展示了如何训练模型以及使用GANs进行图像生成。最后,总结了本文的主要内容和实现步骤。
MachineLP
2018/01/09
1.2K0
tf24: GANs—生成明星脸
EMA算法及其tensorflow实现
滑动平均模型可以使模型在测试数据上更健壮(robust)的方法------滑动平均模型。在采用随机梯度下降算法训练神经网络时,使用滑动平均模型在很多应用中都可以在一定程度提高最终模型在测试数据上的表现。
狼啸风云
2019/08/18
1.4K0
TensorFlow中滑动平均模型介绍
其中a的取值范围[0,1],具体就是:本次滤波结果=(1-a)*本次采样值+a*上次滤波结果,采用此算法的目的是:
老潘
2018/06/21
1.7K0
TensorFlow中滑动平均模型介绍
DSSM & Multi-view DSSM TensorFlow实现
Learning Deep Structured Semantic Models for Web Search using Clickthrough Data以及其后续文章
百川AI
2021/10/19
1.1K0
基于tensorflow的MNIST数字识别
MNIST是一个非常有名的手写体数字识别数据集,在很多资料中,这个数据集都会作为深度学习的入门样例。下面大致介绍这个数据集的基本情况,并介绍temsorflow对MNIST数据集做的封装。tensorflow的封装让使用MNIST数据集变得更加方便。MNIST数据集是NIST数据集的一个子集,它包含了60000张图片作为训练数据,10000张图片作为测试数据。在MNIST数据集中的每一张图片都代表了0~9中的一个数字。图片的大小都为28*28,且数字都会出现在图片的正中间。
狼啸风云
2019/03/01
3.1K0
TensorFlow-CIFAR10 CNN代码分析
想了解更多信息请参考CIFAR-10 page,以及Alex Krizhevsky写的技术报告
百川AI
2021/10/19
7100
TensorFlow-实战Google深度学习框架 笔记(上)
TensorFlow 是一种采用数据流图(data flow graphs),用于数值计算的开源软件库。在 Tensorflow 中,所有不同的变量和运算都是储存在计算图,所以在我们构建完模型所需要的图之后,还需要打开一个会话(Session)来运行整个计算图 通常使用import tensorflow as tf来载入TensorFlow 在TensorFlow程序中,系统会自动维护一个默认的计算图,通过tf.get_default_graph函数可以获取当前默认的计算图。除了使用默认的计算图,可以使用tf.Graph函数来生成新的计算图,不同计算图上的张量和运算不会共享 在TensorFlow程序中,所有数据都通过张量的形式表示,张量可以简单的理解为多维数组,而张量在TensorFlow中的实现并不是直接采用数组的形式,它只是对TensorFlow中运算结果的引用。即在张量中没有真正保存数字,而是如何得到这些数字的计算过程 如果对变量进行赋值的时候不指定类型,TensorFlow会给出默认的类型,同时在进行运算的时候,不会进行自动类型转换 会话(session)拥有并管理TensorFlow程序运行时的所有资源,所有计算完成之后需要关闭会话来帮助系统回收资源,否则可能会出现资源泄漏问题 一个简单的计算过程:
范中豪
2019/09/10
6870
TensorFlow-实战Google深度学习框架 笔记(上)
如何用tensorflow优化神经网络
梯度下降算法主要用户优化单个参数的取值,而反向传播算法给出了一个高效的方式在所有参数上使用梯度下降算法,从而使神经网络模型在训练数据上的损失函数尽可能小。反向传播算法是训练神经网络的核心算法,他可以根据定义好的损失函数优化神经网络中参数的取值,从而使神经网络在训练数据集上的损失函数达到一个最小值。神经网络模型中参数的优化过程直接决定了模型的质量,是使用神经网络时非常重要的一步。
狼啸风云
2019/01/18
1.1K0
10分钟详解EMA(滑动平均)并解决EMA下ckpt权重与pb权重表现不一问题
今天用YunYang的evaluate.py评估模型的时候,意外发现用同样的ckpt权重文件转换而成的pb文件效果不一样,使用ckpt的效果非常差,仔细研究后才发现是滑动平均(EMA)搞的鬼,于是便重新重温了一下EMA。 目录 EMA定义 EMA原理理解 ckpt和pb保存不同的原因 参考 EMA定义与原理 EMA(ExponentialMovingAverage),也就是我们常说的滑动平均模型,一般在采用SGD(随机梯度下降)训练的时候,都会用他来提高我们在测试数据的表现,我们从[1]结合tensorfl
CristianoC
2020/06/02
3K0
Tensorflow 实战笔记
tf.Variable(tf.random_normal([2,3],stddev=2)) 通过满足正态分布的随机数来初始化神经网络中的参数是一个常用方法。
微风、掠过
2018/10/09
5200
Tensorflow 实战笔记
推荐阅读
相关推荐
tensorflow学习笔记(四十一):control dependencies
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档