下载并加载训练数据
Cache file /home/aistudio/.cache/paddle/dataset/mnist/train-images-idx3-ubyte.gz not found, downloading https://dataset.bj.bcebos.com/mnist/train-images-idx3-ubyte.gz
Begin to download
Download finished
Cache file /home/aistudio/.cache/paddle/dataset/mnist/train-labels-idx1-ubyte.gz not found, downloading https://dataset.bj.bcebos.com/mnist/train-labels-idx1-ubyte.gz
Begin to download
........
Download finished
Cache file /home/aistudio/.cache/paddle/dataset/mnist/t10k-images-idx3-ubyte.gz not found, downloading https://dataset.bj.bcebos.com/mnist/t10k-images-idx3-ubyte.gz
Begin to download
Download finished
Cache file /home/aistudio/.cache/paddle/dataset/mnist/t10k-labels-idx1-ubyte.gz not found, downloading https://dataset.bj.bcebos.com/mnist/t10k-labels-idx1-ubyte.gz
Begin to download
..
Download finished
加载完成
AxesImage(18,18;111.6x108.72)
train_data0 的标签为: [5]
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/cbook/__init__.py:2349: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working
if isinstance(obj, collections.Iterator):
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/cbook/__init__.py:2366: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working
return list(data) if isinstance(data, collections.MappingView) else data
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/numpy/lib/type_check.py:546: DeprecationWarning: np.asscalar(a) is deprecated since NumPy v1.16, use a.item() instead
'a.item() instead', DeprecationWarning, stacklevel=1)
# 定义多层感知器 #动态图定义多层感知器classmultilayer_perceptron(paddle.nn.Layer):def__init__(self):super(multilayer_perceptron,self).__init__()#请在这里补全网络代码
self.flatten = paddle.nn.Flatten()
self.linear_1 = paddle.nn.Linear(784,512)
self.linear_2 = paddle.nn.Linear(512,10)
self.relu = paddle.nn.ReLU()
self.dropout = paddle.nn.Dropout(0.2)defforward(self, x):#请在这里补全传播过程的代码
y = self.flatten(x)
y = self.linear_1(y)
y = self.relu(y)
y = self.dropout(y)
y = self.linear_2(y)return y
#请在这里定义卷积网络的代码#注意:定义完成卷积的代码后,后面的代码是需要修改的!
from paddle.metric import Accuracy
LeNet = multilayer_perceptron()# 用Model封装模型
model = paddle.Model(LeNet)# 定义损失函数
optim = paddle.optimizer.Adam(learning_rate=0.001, parameters=model.parameters())# 配置模型
model.prepare(optim,paddle.nn.CrossEntropyLoss(),Accuracy())# 训练保存并验证模型
model.fit(train_dataset,test_dataset,epochs=2,batch_size=64,save_dir='multilayer_perceptron',verbose=1)
The loss value printed in the log is the current step, and the metric is the average value of previous step.
Epoch 1/2
step 30/938 [..............................] - loss: 0.4647 - acc: 0.6240 - ETA: 10s - 12ms/st
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/fluid/layers/utils.py:77: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working
return (isinstance(seq, collections.Sequence) and
step 60/938 [>.............................] - loss: 0.3397 - acc: 0.7237 - ETA: 8s - 9ms/step step 680/938 [====================>.........] - loss: 0.1142 - acc: 0.8893 - ETA: 1s - 7ms/s
step 938/938 [==============================] - loss: 0.3313 - acc: 0.9031 - 7ms/step
save checkpoint at /home/aistudio/multilayer_perceptron/0
Eval begin...
The loss value printed in the log is the current batch, and the metric is the average value of previous step.
step 157/157 [==============================] - loss: 0.0225 - acc: 0.9564 - 6ms/step
Eval samples: 10000
Epoch 2/2
step 938/938 [==============================] - loss: 0.1209 - acc: 0.9503 - 7ms/step
save checkpoint at /home/aistudio/multilayer_perceptron/1
Eval begin...
The loss value printed in the log is the current batch, and the metric is the average value of previous step.
step 157/157 [==============================] - loss: 0.0044 - acc: 0.9683 - 6ms/step
Eval samples: 10000
save checkpoint at /home/aistudio/multilayer_perceptron/final
The loss value printed in the log is the current step, and the metric is the average value of previous step.
Epoch 1/2
step 938/938 [==============================] - loss: 0.2485 - acc: 0.9600 - 7ms/step
save checkpoint at /home/aistudio/multilayer_perceptron/0
Eval begin...
The loss value printed in the log is the current batch, and the metric is the average value of previous step.
step 157/157 [==============================] - loss: 0.0070 - acc: 0.9663 - 6ms/step
Eval samples: 10000
Epoch 2/2
step 938/938 [==============================] - loss: 0.0874 - acc: 0.9647 - 7ms/step
save checkpoint at /home/aistudio/multilayer_perceptron/1
Eval begin...
The loss value printed in the log is the current batch, and the metric is the average value of previous step.
step 157/157 [==============================] - loss: 7.2327e-04 - acc: 0.9687 - 6ms/step
Eval samples: 10000
save checkpoint at /home/aistudio/multilayer_perceptron/final
第一步,把当前路径转换到data目录,可以使用命令!cd data。在AI studio nootbook中可以使用Linux命令,需要在命令的最前面加上英文的感叹号(!)。用&&可以连接两个命令。用\号可以换行写代码。需要注意的是,每次重新打开该项目,data文件夹下除了挂载的数据集,其他文件都会被清空。因此,如果把数据保存在data目录中,每次重新启动项目时,都需要解压缩一下。如果想省事持久化保存,可以把数据保存在work目录下。
#以下代码用于建立样本数据读取路径与样本标签之间的关系import os
import random
data_list =[]#用个列表保存每个样本的读取路径、标签#由于属种名称本身是字符串,而输入模型的是数字。需要构造一个字典,把某个数字代表该属种名称。键是属种名称,值是整数。
label_list=[]withopen("/home/aistudio/data/species.txt")as f:for line in f:
a,b = line.strip("\n").split(" ")
label_list.append([b,int(a)-1])
label_dic =dict(label_list)#获取Butterfly20目录下的所有子目录名称,保存进一个列表之中
class_list = os.listdir("/home/aistudio/data/Butterfly20")
class_list.remove('.DS_Store')#删掉列表中名为.DS_Store的元素,因为.DS_Store并没有样本。for each in class_list:for f in os.listdir("/home/aistudio/data/Butterfly20/"+each):
data_list.append(["/home/aistudio/data/Butterfly20/"+each+'/'+f,label_dic[each]])#按文件顺序读取,可能造成很多属种图片存在序列相关,用random.shuffle方法把样本顺序彻底打乱。
random.shuffle(data_list)#打印前十个,可以看出data_list列表中的每个元素是[样本读取路径, 样本标签]。print(data_list[0:10])#打印样本数量,一共有1866个样本。print("样本数量是:{}".format(len(data_list)))
2021-03-10 14:33:34,745 - INFO - unique_endpoints {''}
2021-03-10 14:33:34,747 - INFO - File /home/aistudio/.cache/paddle/hapi/weights/resnet50.pdparams md5 checking...
2021-03-10 14:33:35,106 - INFO - Found /home/aistudio/.cache/paddle/hapi/weights/resnet50.pdparams
The loss value printed in the log is the current step, and the metric is the average value of previous step.
Epoch 1/100
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/fluid/layers/utils.py:77: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working
return (isinstance(seq, collections.Sequence) and
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/nn/layer/norm.py:648: UserWarning: When training, we now always track global mean and variance.
"When training, we now always track global mean and variance.")
step 24/24 [==============================] - loss: 1.6762 - acc: 0.3362 - 497ms/step
save checkpoint at /home/aistudio/res101/0
Eval begin...
The loss value printed in the log is the current batch, and the metric is the average value of previous step.
step 6/6 [==============================] - loss: 2.0643 - acc: 0.5201 - 497ms/step
Eval samples: 373
Epoch 2/100
step 24/24 [==============================] - loss: 0.8775 - acc: 0.7220 - 488ms/step
Eval begin...
The loss value printed in the log is the current batch, and the metric is the average value of previous step.
step 6/6 [==============================] - loss: 1.5468 - acc: 0.6595 - 494ms/step
Eval samples: 373
......(省略)
Eval begin...
The loss value printed in the log is the current batch, and the metric is the average value of previous step.
step 6/6 [==============================] - loss: 1.2660 - acc: 0.8525 - 498ms/step
Eval samples: 373
Epoch 58: Early stopping.
Best checkpoint has been saved at /home/aistudio/res101/best_model
save checkpoint at /home/aistudio/res101/final
result = model.evaluate(eval_dataset, verbose=1)print(result)
Eval begin...
The loss value printed in the log is the current batch, and the metric is the average value of previous step.
step 373/373 [==============================] - 30ms/step
Eval samples: 373
{}
# 导入所需要的库from sklearn.utils import shuffle
import os
import pandas as pd
import numpy as np
from PIL import Image
import paddle
import paddle.nn as nn
from paddle.io import Dataset
import paddle.vision.transforms as T
import paddle.nn.functional as F
from paddle.metric import Accuracy
import warnings
warnings.filterwarnings("ignore")# 读取数据
train_images = pd.read_csv('lemon_lesson/train_images.csv', usecols=['id','class_num'])# labelshufflingdeflabelShuffling(dataFrame, groupByName ='class_num'):
groupDataFrame = dataFrame.groupby(by=[groupByName])
labels = groupDataFrame.size()print("length of label is ",len(labels))
maxNum =max(labels)
lst = pd.DataFrame()for i inrange(len(labels)):print("Processing label :", i)
tmpGroupBy = groupDataFrame.get_group(i)
createdShuffleLabels = np.random.permutation(np.array(range(maxNum)))% labels[i]print("Num of the label is : ", labels[i])
lst=lst.append(tmpGroupBy.iloc[createdShuffleLabels], ignore_index=True)print("Done")# lst.to_csv('test1.csv', index=False)return lst
# 划分训练集和校验集
all_size =len(train_images)# print(all_size)
train_size =int(all_size *0.8)
train_image_list = train_images[:train_size]
val_image_list = train_images[train_size:]
df = labelShuffling(train_image_list)
df = shuffle(df)
train_image_path_list = df['id'].values
label_list = df['class_num'].values
label_list = paddle.to_tensor(label_list, dtype='int64')
train_label_list = paddle.nn.functional.one_hot(label_list, num_classes=4)
val_image_path_list = val_image_list['id'].values
val_label_list = val_image_list['class_num'].values
val_label_list = paddle.to_tensor(val_label_list, dtype='int64')
val_label_list = paddle.nn.functional.one_hot(val_label_list, num_classes=4)# 定义数据预处理
data_transforms = T.Compose([
T.Resize(size=(224,224)),
T.RandomHorizontalFlip(224),
T.RandomVerticalFlip(224),
T.Transpose(),# HWC -> CHW
T.Normalize(
mean=[0,0,0],# 归一化
std=[255,255,255],
to_rgb=True)])
length of label is 4
Processing label : 0
Num of the label is : 321
Done
Processing label : 1
Num of the label is : 207
Done
Processing label : 2
Num of the label is : 181
Done
Processing label : 3
Num of the label is : 172
Done
2021-03-10 13:10:11,146 - INFO - unique_endpoints {''}
2021-03-10 13:10:11,147 - INFO - File /home/aistudio/.cache/paddle/hapi/weights/mobilenet_v2_x1.0.pdparams md5 checking...
2021-03-10 13:10:11,195 - INFO - Found /home/aistudio/.cache/paddle/hapi/weights/mobilenet_v2_x1.0.pdparams
The loss value printed in the log is the current step, and the metric is the average value of previous step.
Epoch 1/10
step 41/41 [==============================] - loss: 0.5766 - acc: 0.9221 - 305ms/step
Eval begin...
The loss value printed in the log is the current batch, and the metric is the average value of previous step.
step 41/41 [==============================] - loss: 0.3831 - acc: 1.0000 - 289ms/step
Eval samples: 1284
Epoch 2/10
step 41/41 [==============================] - loss: 0.7965 - acc: 0.9977 - 299ms/step
......(省略)
Eval begin...
The loss value printed in the log is the current batch, and the metric is the average value of previous step.
step 41/41 [==============================] - loss: 0.3626 - acc: 1.0000 - 292ms/step
Eval samples: 1284
以教员和课程为例介绍一对多关联关系,在这里认为一个教员可以叫多门课程,而一门课程只有1个教员教,这种关系在实际中不太常见,通过教员和课程是多对多的关系。
示例数据:
地址表:
CREATE TABLE ADDRESSES
(
ADDR_ID INT(11) NOT NULL AUTO_INCREMENT,
STREET VAR
In this lesson we used the key "UITextAttributeTextColor" to change the color of the UINavigationBar appearance to white. This prompts a warning "first deprecated in iOS 7.0."
Ins
质数也叫素数,是只能被1和它本身整除的正整数,最小的质数是2,目前发现的最大的质数是p=2^57885161-1【注1】。
判断一个数是质数的最简单的方法如下:
def isPrime1(n):
for i in range(2, n):
if n % i == 0:
return False
return True
但是在上面的方法中有一些冗余的计算,所以
hbase(hadoop)是用java编写的,有些语言(例如python)能够对它提供良好的支持,但也有很多语言使用起来并不是那么方便,比如c#只能通过thrift访问。Rest就能很好的解决这个问题。Hbase的org.apache.hadoop.hbase.rest包提供了rest接口,它内嵌了jetty作为servlet容器。
启动命令:./bin/hbase rest s
下面这段sql本来目的是想更新条件下的数据,可是这段sql却更新了整个表的数据。sql如下:
UPDATE tops_visa.visa_order
SET op_audit_abort_pass_date = now()
FROM
tops_visa.visa_order as t1
INNER JOIN tops_visa.visa_visitor as t2
ON t1.