├── face ├── .gitignore ├── opencv_face.py └── find_face.py ├── ncs ├── .gitignore ├── convert-vgg16.py ├── convert-mnist.py ├── predict-vgg16.py ├── predict-mnist.py ├── opencv-vgg16.py ├── train-minst.py └── README.md ├── opencv.js ├── opencv │ └── opencv_js.wasm ├── Readme.md ├── index.html └── face.js ├── paddle ├── mnist-gan-visualdl.png ├── print.ipynb ├── dropout.ipynb ├── inference_model.ipynb ├── variable.ipynb ├── initializer.ipynb ├── shared_params.ipynb ├── trainer_executor.ipynb ├── recordio.ipynb ├── view_saved_params.ipynb ├── pretrained.ipynb └── startup_program.ipynb ├── requirements.txt ├── .gitignore ├── text-classification ├── encoder.py ├── text-cnn.py └── fast-text.py └── gan └── dcgan.py /face/.gitignore: -------------------------------------------------------------------------------- 1 | face-images -------------------------------------------------------------------------------- /ncs/.gitignore: -------------------------------------------------------------------------------- 1 | output* 2 | *.h5 3 | *.json 4 | logs 5 | TF_Model 6 | graph 7 | data -------------------------------------------------------------------------------- /opencv.js/opencv/opencv_js.wasm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oraoto/learn_ml/HEAD/opencv.js/opencv/opencv_js.wasm -------------------------------------------------------------------------------- /paddle/mnist-gan-visualdl.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oraoto/learn_ml/HEAD/paddle/mnist-gan-visualdl.png -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | jupyter 2 | numpy 3 | pandas 4 | matplotlib 5 | tensorflow 6 | keras 7 | sklearn 8 | nnabla 9 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.h5 2 | *.csv 3 | *.txt 4 | 5 | settings.json 6 | 7 | gan-mnist 8 | sklearn 9 | chatterbot 10 | mkl-dnn 11 | mmdnn 12 | -------------------------------------------------------------------------------- /opencv.js/Readme.md: -------------------------------------------------------------------------------- 1 | # OpenCV.js face detection 2 | 3 | 4 | ## Run 5 | 6 | Start a static file server, eg: 7 | 8 | ``` 9 | php -S 0.0.0.0:8000 -t . 10 | ``` 11 | 12 | Open your browser, and visit `http://127.0.0.1:8000`. 13 | 14 | Tested in Chrome 65 and Firefox 60. 15 | 16 | -------------------------------------------------------------------------------- /ncs/convert-vgg16.py: -------------------------------------------------------------------------------- 1 | #%% 2 | from keras.applications import VGG16 3 | from keras import backend as K 4 | import tensorflow as tf 5 | 6 | #K.set_learning_phase(0) 7 | mn = VGG16() 8 | saver = tf.train.Saver() 9 | sess = K.get_session() 10 | saver.save(sess, "./TF_Model/vgg16") 11 | 12 | fw = tf.summary.FileWriter('logs', sess.graph) 13 | fw.close() 14 | 15 | # mvNCProfile TF_Model/vgg16.meta -in=input_1 -on=predictions/Softmax -s 12 -------------------------------------------------------------------------------- /opencv.js/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | OpenCV.js Face Detection 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /ncs/convert-mnist.py: -------------------------------------------------------------------------------- 1 | from keras.models import model_from_json 2 | from keras import backend as K 3 | import tensorflow as tf 4 | 5 | model_file = "model.json" 6 | weights_file = "weights.h5" 7 | 8 | with open(model_file, "r") as file: 9 | config = file.read() 10 | 11 | K.set_learning_phase(0) 12 | model = model_from_json(config) 13 | model.load_weights(weights_file) 14 | 15 | saver = tf.train.Saver() 16 | sess = K.get_session() 17 | saver.save(sess, "./TF_Model/tf_model") 18 | 19 | fw = tf.summary.FileWriter('logs', sess.graph) 20 | fw.close() 21 | -------------------------------------------------------------------------------- /ncs/predict-vgg16.py: -------------------------------------------------------------------------------- 1 | from mvnc import mvncapi as mvnc 2 | import cv2 3 | import numpy 4 | import sys 5 | from keras.applications import VGG16 6 | from keras.applications.vgg16 import preprocess_input, decode_predictions 7 | from keras.preprocessing.image import load_img, img_to_array 8 | import matplotlib.pyplot as plt 9 | 10 | img = load_img(sys.argv[1], target_size=(224, 224)) 11 | img = img_to_array(img) 12 | img = preprocess_input(img) 13 | 14 | # print("VGG16") 15 | 16 | # vgg16 = VGG16() 17 | # result = vgg16.predict(img.reshape(1, 224, 224, 3)) 18 | # print(decode_predictions(result)) 19 | 20 | print("NCS") 21 | 22 | devices = mvnc.EnumerateDevices() 23 | 24 | device = mvnc.Device(devices[0]) 25 | device.OpenDevice() 26 | 27 | with open("graph", mode='rb') as f: 28 | graphfile = f.read() 29 | 30 | graph = device.AllocateGraph(graphfile) 31 | 32 | graph.LoadTensor(img.astype(numpy.float16), 'user object') 33 | output, userobj = graph.GetResult() 34 | 35 | graph.DeallocateGraph() 36 | device.CloseDevice() 37 | 38 | result = decode_predictions(output.reshape(1, 1000)) 39 | print(result) 40 | -------------------------------------------------------------------------------- /face/opencv_face.py: -------------------------------------------------------------------------------- 1 | #%% 2 | import face_recognition 3 | import sys 4 | import os 5 | from PIL import Image, ImageDraw 6 | import cv2 7 | import numpy as np 8 | import time 9 | 10 | cap = cv2.VideoCapture(0) 11 | 12 | while(True): 13 | ret, frame = cap.read() 14 | 15 | scale = 4 16 | 17 | small_frame = cv2.resize(frame, (0, 0), fx=1/scale, fy=1/scale) 18 | 19 | face_locations = face_recognition.face_locations(small_frame, 1, model="hog") 20 | 21 | pil_image = Image.fromarray(frame) 22 | draw = ImageDraw.Draw(pil_image) 23 | 24 | for (top, right, bottom, left) in face_locations: 25 | top *= scale 26 | right *= scale 27 | bottom *= scale 28 | left *= scale 29 | padding = (right - left) // 4 30 | draw.rectangle((max(left - padding // 4, 0), max(top - padding * 2, 0), (right + padding // 4, bottom + padding // 4)), outline=(0, 0, 255)) 31 | draw.rectangle(((left, top), (right, bottom)), outline=(255, 0, 0)) 32 | del draw 33 | 34 | img = np.array(pil_image) 35 | cv2.imshow('frame', img) 36 | 37 | if cv2.waitKey(1) & 0xFF == ord('q'): 38 | break 39 | 40 | cap.release() 41 | cv2.destroyAllWindows() 42 | -------------------------------------------------------------------------------- /face/find_face.py: -------------------------------------------------------------------------------- 1 | #%% 2 | import face_recognition 3 | import sys 4 | import os 5 | from PIL import Image, ImageDraw 6 | 7 | #%% 8 | def find_face(path, crop): 9 | print(path) 10 | image = face_recognition.load_image_file(path) 11 | face_locations = face_recognition.face_locations(image, 2, model="hog") 12 | 13 | print(face_locations) 14 | 15 | pil_image = Image.fromarray(image) 16 | 17 | 18 | draw = ImageDraw.Draw(pil_image) 19 | 20 | for (top, right, bottom, left) in face_locations: 21 | padding = (right - left) // 4 22 | if crop: 23 | box = (max(left - padding // 4, 0), max(top - padding * 2, 0), right + padding // 4, bottom + padding // 4) 24 | pil_image = pil_image.crop(box) 25 | break 26 | draw.rectangle((max(left - padding // 4, 0), max(top - padding * 2, 0), (right + padding // 4, bottom + padding // 4)), outline=(0, 0, 255)) 27 | draw.rectangle(((left, top), (right, bottom)), outline=(255, 0, 0)) 28 | del draw 29 | 30 | return pil_image 31 | 32 | if __name__ == '__main__': 33 | crop = len(sys.argv) > 2 and sys.argv[2] == "c" 34 | img = find_face(sys.argv[1], crop) 35 | 36 | img.show() 37 | -------------------------------------------------------------------------------- /ncs/predict-mnist.py: -------------------------------------------------------------------------------- 1 | from keras import layers 2 | from keras import models 3 | from keras.datasets import mnist 4 | from keras.utils import to_categorical 5 | from keras import backend as K 6 | import tensorflow as tf 7 | from mvnc import mvncapi as mvnc 8 | import numpy 9 | 10 | (x_train, y_train), (x_test, y_test) = mnist.load_data() 11 | 12 | # Test image 13 | test_idx = numpy.random.randint(0, 10000) 14 | test_image = x_test[test_idx] 15 | test_image = test_image.astype('float32') / 255.0 16 | 17 | ## With Keras 18 | model_file = "model.json" 19 | weights_file = "weights.h5" 20 | 21 | with open(model_file, "r") as file: 22 | config = file.read() 23 | model = models.model_from_json(config) 24 | model.load_weights(weights_file) 25 | result = model.predict(test_image.reshape(1, 28, 28, 1))[0] 26 | print("Keras", result, result.argmax()) 27 | 28 | # With NCS 29 | devices = mvnc.EnumerateDevices() 30 | device = mvnc.Device(devices[0]) 31 | device.OpenDevice() 32 | 33 | with open("graph", mode='rb') as f: 34 | graphfile = f.read() 35 | 36 | graph = device.AllocateGraph(graphfile) 37 | 38 | graph.LoadTensor(test_image.astype('float16'), 'user object') 39 | 40 | output, userobj = graph.GetResult() 41 | 42 | graph.DeallocateGraph() 43 | device.CloseDevice() 44 | 45 | print("NCS", output, output.argmax()) 46 | print("Correct", y_test[test_idx]) 47 | -------------------------------------------------------------------------------- /ncs/opencv-vgg16.py: -------------------------------------------------------------------------------- 1 | from mvnc import mvncapi as mvnc 2 | import cv2 3 | import numpy 4 | import sys 5 | from keras.applications import VGG16 6 | from keras.applications.vgg16 import preprocess_input, decode_predictions 7 | from keras.preprocessing.image import load_img, img_to_array 8 | import matplotlib.pyplot as plt 9 | import time 10 | 11 | # NCS 12 | devices = mvnc.EnumerateDevices() 13 | 14 | device = mvnc.Device(devices[0]) 15 | device.OpenDevice() 16 | 17 | with open("graph", mode='rb') as f: 18 | graphfile = f.read() 19 | 20 | graph = device.AllocateGraph(graphfile) 21 | 22 | # OpenCV 23 | cap = cv2.VideoCapture(0) 24 | while(True): 25 | ret, frame = cap.read() 26 | 27 | img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) 28 | img = cv2.resize(img, (224, 224)) 29 | img = preprocess_input(img.astype('float32')) 30 | 31 | graph.LoadTensor(img.astype(numpy.float16), 'user object') 32 | 33 | output, userobj = graph.GetResult() 34 | 35 | result = decode_predictions(output.reshape(1, 1000)) 36 | 37 | print(result[0]) 38 | 39 | frame = cv2.putText(frame, str(result[0][0]), (0, 15), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,255,0), 2) 40 | cv2.imshow('frame', frame) 41 | 42 | if cv2.waitKey(1) & 0xFF == ord('q'): 43 | break 44 | 45 | cap.release() 46 | cv2.destroyAllWindows() 47 | 48 | graph.DeallocateGraph() 49 | device.CloseDevice() 50 | -------------------------------------------------------------------------------- /ncs/train-minst.py: -------------------------------------------------------------------------------- 1 | #%% 2 | from keras import layers 3 | from keras import models 4 | from keras.datasets import mnist 5 | from keras.utils import to_categorical 6 | from keras import backend as K 7 | import tensorflow as tf 8 | 9 | #%% 10 | (x_train, y_train), (x_test, y_test) = mnist.load_data() 11 | 12 | x_train = x_train.astype('float32') / 255 13 | x_test = x_test.astype('float32') / 255 14 | x_train = x_train.reshape(-1, 28, 28, 1) 15 | x_test = x_test.reshape(-1, 28, 28, 1) 16 | 17 | y_train = to_categorical(y_train) 18 | y_test = to_categorical(y_test) 19 | 20 | #%% 21 | cnn = models.Sequential() 22 | cnn.add(layers.Conv2D(16, 3, activation='relu', input_shape=(28, 28, 1))) 23 | cnn.add(layers.MaxPool2D()) 24 | cnn.add(layers.Conv2D(32, 3, activation='relu')) 25 | cnn.add(layers.MaxPool2D()) 26 | cnn.add(layers.Conv2D(64, 3, activation='relu')) 27 | cnn.add(layers.MaxPool2D()) 28 | cnn.add(layers.Flatten()) 29 | cnn.add(layers.Dense(256, activation='relu')) 30 | cnn.add(layers.Dropout(0.5)) 31 | cnn.add(layers.Dense(10, activation='softmax')) 32 | cnn.summary() 33 | 34 | cnn.compile(optimizer='adam', metrics=['accuracy'], loss='categorical_crossentropy') 35 | 36 | history = cnn.fit(x_train, y_train, epochs=10, batch_size=128) 37 | 38 | print(cnn.evaluate(x_test, y_test)) 39 | 40 | #%% 41 | with open("model.json", "w") as file: 42 | file.write(cnn.to_json()) 43 | cnn.save_weights("weights.h5") 44 | -------------------------------------------------------------------------------- /text-classification/encoder.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | import numpy as np 3 | from sklearn.preprocessing import LabelEncoder 4 | import pickle 5 | import os 6 | 7 | def read_file(path): 8 | with open(path, encoding="utf-8") as file: 9 | data = [line.split("\t") for line in file.readlines()] 10 | return pd.DataFrame(data, columns=["category", "text"]) 11 | 12 | class Encoder: 13 | def __init__(self, text_len=600): 14 | self.text_len = text_len 15 | self.wtoi = {} 16 | self.itow = {} 17 | self.label_encoder = LabelEncoder() 18 | 19 | def fit(self, X): 20 | for _, words in X['text'].items(): 21 | words = list(str(words)) 22 | for w in words: 23 | if w not in self.wtoi: 24 | i = len(self.wtoi) 25 | self.wtoi[w] = i 26 | self.itow[i] = w 27 | self.label_encoder.fit(X['category']) 28 | 29 | def transform(self, X): 30 | text_dataset = [] 31 | 32 | for _, words in X['text'].items(): 33 | row = [] 34 | for w in words: 35 | if w not in self.wtoi: 36 | i = 0 37 | else: 38 | i = self.wtoi[w] 39 | row.append(i) 40 | if len(row) > self.text_len: 41 | row[:self.text_len] 42 | text_dataset.append(np.resize(np.array(row), (self.text_len,))) # padding不是填0 43 | 44 | texts = np.stack(text_dataset) 45 | labels = self.label_encoder.transform(X['category']) 46 | return texts, labels 47 | 48 | train_data = read_file("./data/cnews.train.txt") 49 | validate_data = read_file("./data/cnews.val.txt") 50 | test_data = read_file("./data/cnews.test.txt") 51 | 52 | encoder = Encoder() 53 | 54 | if os.path.exists("fast-text-encoder.pkl"): 55 | with open("fast-text-encoder.pkl", "rb") as f: 56 | encoder = pickle.load(f) 57 | else: 58 | encoder = Encoder() 59 | encoder.fit(train_data) 60 | with open("fast-text-encoder.pkl", "wb") as f: 61 | encoder = pickle.dump(encoder, f) 62 | 63 | train_data, train_labels = encoder.transform(train_data) 64 | validate_data, validate_labels = encoder.transform(validate_data) 65 | test_data, test_labels = encoder.transform(test_data) 66 | -------------------------------------------------------------------------------- /opencv.js/face.js: -------------------------------------------------------------------------------- 1 | let videoHeight = 480 2 | let videoWidth = 640 3 | let outputCanvas = document.getElementById("outputCanvas"); 4 | 5 | let cap = null 6 | let faceCascade = null; 7 | let src = null; 8 | let gray = null; 9 | 10 | function run() { 11 | 12 | faceCascade = new cv.CascadeClassifier(); 13 | faceCascade.load("face.xml") 14 | 15 | cap = new cv.VideoCapture(video) 16 | src = new cv.Mat(videoHeight, videoWidth, cv.CV_8UC4); 17 | gray = new cv.Mat(videoHeight, videoWidth, cv.CV_8UC1); 18 | 19 | startCamera(); 20 | requestAnimationFrame(detectFace) 21 | } 22 | 23 | async function startCamera() { 24 | let video = document.getElementById("video"); 25 | let stream = await navigator.mediaDevices.getUserMedia({ 26 | video: { 27 | width: { 28 | exact: videoWidth 29 | }, 30 | height: { 31 | exact: videoHeight 32 | } 33 | }, 34 | audio: false 35 | }) 36 | video.srcObject = stream; 37 | video.play(); 38 | } 39 | 40 | function detectFace() { 41 | // Capture a frame 42 | cap.read(src) 43 | 44 | // Convert to greyscale 45 | cv.cvtColor(src, gray, cv.COLOR_RGBA2GRAY); 46 | 47 | 48 | // Downsample 49 | let downSampled = new cv.Mat(); 50 | cv.pyrDown(gray, downSampled); 51 | cv.pyrDown(downSampled, downSampled); 52 | 53 | // Detect faces 54 | let faces = new cv.RectVector(); 55 | faceCascade.detectMultiScale(downSampled, faces) 56 | 57 | // Draw boxes 58 | let size = downSampled.size(); 59 | let xRatio = videoWidth / size.width; 60 | let yRatio = videoHeight / size.height; 61 | for (let i = 0; i < faces.size(); ++i) { 62 | let face = faces.get(i); 63 | let point1 = new cv.Point(face.x * xRatio, face.y * yRatio); 64 | let point2 = new cv.Point((face.x + face.width) * xRatio, (face.y + face.height) * xRatio); 65 | cv.rectangle(src, point1, point2, [255, 0, 0, 255]) 66 | } 67 | // Free memory 68 | downSampled.delete() 69 | faces.delete() 70 | 71 | // Show image 72 | cv.imshow(outputCanvas, src) 73 | 74 | requestAnimationFrame(detectFace) 75 | } 76 | 77 | // Config OpenCV 78 | var Module = { 79 | locateFile: function (name) { 80 | let files = { 81 | "opencv_js.wasm": '/opencv/opencv_js.wasm' 82 | } 83 | return files[name] 84 | }, 85 | preRun: [() => { 86 | Module.FS_createPreloadedFile("/", "face.xml", "model/haarcascade_frontalface_default.xml", 87 | true, false); 88 | }], 89 | postRun: [ 90 | run 91 | ] 92 | }; -------------------------------------------------------------------------------- /ncs/README.md: -------------------------------------------------------------------------------- 1 | # Intel® Movidius™ NCS examples 2 | 3 | ## MNIST with Keras 4 | 5 | Train a simple CNN for MNIST: 6 | 7 | ``` 8 | $ python train-mnist.py 9 | ``` 10 | 11 | Convert Keras model to Tensorflow model 12 | 13 | ``` 14 | $ python convert-mnist.py 15 | ``` 16 | 17 | Check, Compile, Profile 18 | 19 | ``` 20 | $ mvNCCheck TF_Model/tf_model.meta -in=conv2d_1_input -on=dense_2/Softmax 21 | $ mvNCCompile TF_Model/tf_model.meta -in=conv2d_1_input -on=dense_2/Softmax 22 | $ mvNCProfile TF_Model/tf_model.meta -in=conv2d_1_input -on=dense_2/Softmax 23 | ``` 24 | 25 | Do prediction on a random image: 26 | 27 | ``` 28 | $ python predict-mnist.py 29 | ``` 30 | 31 | ## VGG16 with Keras 32 | 33 | Convert the VGG16 model to TensorFlow model 34 | 35 | ``` 36 | $ python convert-vgg16.py 37 | ``` 38 | 39 | Check, Compile, Profile 40 | 41 | ``` 42 | $ mvNCCheck TF_Model/vgg16.meta -in=input_1 -on=predictions/Softmax -s 12 43 | $ mvNCCompile TF_Model/vgg16.meta -in=input_1 -on=predictions/Softmax -s 12 44 | $ mvNCProfile TF_Model/vgg16.meta -in=input_1 -on=predictions/Softmax -s 12 45 | ``` 46 | 47 | Profile output: 48 | 49 | ``` 50 | Detailed Per Layer Profile 51 | Bandwidth time 52 | # Name MFLOPs (MB/s) (ms) 53 | ================================================================= 54 | 0 block1_conv1/Relu 173.4 304.1 8.512 55 | 1 block1_conv2/Relu 3699.4 664.6 83.057 56 | 2 block1_pool/MaxPool 3.2 831.7 7.365 57 | 3 block2_conv1/Relu 1849.7 419.9 33.161 58 | 4 block2_conv2/Relu 3699.4 473.8 58.769 59 | 5 block2_pool/MaxPool 1.6 923.5 3.316 60 | 6 block3_conv1/Relu 1849.7 174.3 42.795 61 | 7 block3_conv2/Relu 3699.4 179.7 82.962 62 | 8 block3_conv3/Relu 3699.4 180.9 82.437 63 | 9 block3_pool/MaxPool 0.8 931.8 1.644 64 | 10 block4_conv1/Relu 1849.7 136.1 41.907 65 | 11 block4_conv2/Relu 3699.4 170.0 67.074 66 | 12 block4_conv3/Relu 3699.4 170.2 66.985 67 | 13 block4_pool/MaxPool 0.4 907.0 0.845 68 | 14 block5_conv1/Relu 924.8 312.0 19.970 69 | 15 block5_conv2/Relu 924.8 315.8 19.730 70 | 16 block5_conv3/Relu 924.8 312.7 19.929 71 | 17 block5_pool/MaxPool 0.1 894.4 0.215 72 | 18 fc1/Relu 205.5 2158.5 90.830 73 | 19 fc2/Relu 33.6 2137.5 14.978 74 | 20 predictions/BiasAdd 8.2 2642.3 2.960 75 | 21 predictions/Softmax 0.0 19.0 0.201 76 | ----------------------------------------------------------------- 77 | Total inference time 749.64 78 | ----------------------------------------------------------------- 79 | Generating Profile Report 'output_report.html'... 80 | ``` 81 | 82 | Do prediction on an image: 83 | 84 | ``` 85 | $ python predict-vgg16.py ~/Downloads/th.jpeg 86 | ``` 87 | 88 | ## Reference 89 | 90 | + [ardamavi/Intel-Movidius-NCS-Keras](https://github.com/ardamavi/Intel-Movidius-NCS-Keras) 91 | -------------------------------------------------------------------------------- /paddle/print.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": { 7 | "ExecuteTime": { 8 | "end_time": "2018-07-19T05:00:08.920300Z", 9 | "start_time": "2018-07-19T05:00:08.164544Z" 10 | } 11 | }, 12 | "outputs": [], 13 | "source": [ 14 | "import paddle.fluid as fluid\n", 15 | "import paddle\n", 16 | "import numpy as np\n", 17 | "from paddle.fluid.debugger import draw_block_graphviz" 18 | ] 19 | }, 20 | { 21 | "cell_type": "markdown", 22 | "metadata": { 23 | "ExecuteTime": { 24 | "end_time": "2018-07-19T04:52:01.569854Z", 25 | "start_time": "2018-07-19T04:52:01.563640Z" 26 | } 27 | }, 28 | "source": [ 29 | "## Print" 30 | ] 31 | }, 32 | { 33 | "cell_type": "code", 34 | "execution_count": 2, 35 | "metadata": { 36 | "ExecuteTime": { 37 | "end_time": "2018-07-19T05:00:08.929383Z", 38 | "start_time": "2018-07-19T05:00:08.924284Z" 39 | } 40 | }, 41 | "outputs": [], 42 | "source": [ 43 | "data = fluid.layers.fill_constant(shape=[3, 3], value=9, dtype='int64')" 44 | ] 45 | }, 46 | { 47 | "cell_type": "code", 48 | "execution_count": 3, 49 | "metadata": { 50 | "ExecuteTime": { 51 | "end_time": "2018-07-19T05:00:09.070212Z", 52 | "start_time": "2018-07-19T05:00:08.933126Z" 53 | } 54 | }, 55 | "outputs": [], 56 | "source": [ 57 | "data = fluid.layers.Print(data, message=\"constant data\")" 58 | ] 59 | }, 60 | { 61 | "cell_type": "code", 62 | "execution_count": 4, 63 | "metadata": { 64 | "ExecuteTime": { 65 | "end_time": "2018-07-19T05:00:09.158097Z", 66 | "start_time": "2018-07-19T05:00:09.077309Z" 67 | } 68 | }, 69 | "outputs": [ 70 | { 71 | "data": { 72 | "text/plain": [ 73 | "[]" 74 | ] 75 | }, 76 | "execution_count": 4, 77 | "metadata": {}, 78 | "output_type": "execute_result" 79 | } 80 | ], 81 | "source": [ 82 | "exe = fluid.executor.Executor(fluid.CPUPlace())\n", 83 | "exe.run(fluid.default_startup_program())" 84 | ] 85 | }, 86 | { 87 | "cell_type": "code", 88 | "execution_count": 5, 89 | "metadata": { 90 | "ExecuteTime": { 91 | "end_time": "2018-07-19T05:00:09.273479Z", 92 | "start_time": "2018-07-19T05:00:09.163850Z" 93 | } 94 | }, 95 | "outputs": [ 96 | { 97 | "data": { 98 | "text/plain": [ 99 | "[array([[9, 9, 9],\n", 100 | " [9, 9, 9],\n", 101 | " [9, 9, 9]])]" 102 | ] 103 | }, 104 | "execution_count": 5, 105 | "metadata": {}, 106 | "output_type": "execute_result" 107 | } 108 | ], 109 | "source": [ 110 | "exe.run(fetch_list=[data])" 111 | ] 112 | }, 113 | { 114 | "cell_type": "markdown", 115 | "metadata": {}, 116 | "source": [ 117 | "In stderr:\n", 118 | "\n", 119 | "```\n", 120 | "1531976409\tconstant data\tTensor[fill_constant_0.tmp_0]\n", 121 | "\tshape: [3,3,]\n", 122 | "\tdtype: l\n", 123 | "\tdata: 9,9,9,9,9,9,9,9,9,\n", 124 | "```" 125 | ] 126 | }, 127 | { 128 | "cell_type": "code", 129 | "execution_count": null, 130 | "metadata": {}, 131 | "outputs": [], 132 | "source": [] 133 | } 134 | ], 135 | "metadata": { 136 | "kernelspec": { 137 | "display_name": "Python 2", 138 | "language": "python", 139 | "name": "python2" 140 | }, 141 | "language_info": { 142 | "codemirror_mode": { 143 | "name": "ipython", 144 | "version": 2 145 | }, 146 | "file_extension": ".py", 147 | "mimetype": "text/x-python", 148 | "name": "python", 149 | "nbconvert_exporter": "python", 150 | "pygments_lexer": "ipython2", 151 | "version": "2.7.15" 152 | } 153 | }, 154 | "nbformat": 4, 155 | "nbformat_minor": 2 156 | } 157 | -------------------------------------------------------------------------------- /paddle/dropout.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": { 7 | "ExecuteTime": { 8 | "end_time": "2018-08-03T02:59:33.686492Z", 9 | "start_time": "2018-08-03T02:59:32.916754Z" 10 | } 11 | }, 12 | "outputs": [], 13 | "source": [ 14 | "import paddle.fluid as fluid\n", 15 | "import paddle\n", 16 | "import numpy as np" 17 | ] 18 | }, 19 | { 20 | "cell_type": "code", 21 | "execution_count": 2, 22 | "metadata": { 23 | "ExecuteTime": { 24 | "end_time": "2018-08-03T02:59:33.696253Z", 25 | "start_time": "2018-08-03T02:59:33.690562Z" 26 | } 27 | }, 28 | "outputs": [], 29 | "source": [ 30 | "x = fluid.layers.data('x', shape=[10], dtype='float32')\n", 31 | "y = fluid.layers.dropout(x, dropout_prob=0.3)" 32 | ] 33 | }, 34 | { 35 | "cell_type": "code", 36 | "execution_count": 3, 37 | "metadata": { 38 | "ExecuteTime": { 39 | "end_time": "2018-08-03T02:59:33.765135Z", 40 | "start_time": "2018-08-03T02:59:33.700084Z" 41 | } 42 | }, 43 | "outputs": [], 44 | "source": [ 45 | "test_program = fluid.default_main_program().clone(for_test=True)" 46 | ] 47 | }, 48 | { 49 | "cell_type": "code", 50 | "execution_count": 4, 51 | "metadata": { 52 | "ExecuteTime": { 53 | "end_time": "2018-08-03T02:59:33.857925Z", 54 | "start_time": "2018-08-03T02:59:33.771001Z" 55 | } 56 | }, 57 | "outputs": [ 58 | { 59 | "data": { 60 | "text/plain": [ 61 | "[]" 62 | ] 63 | }, 64 | "execution_count": 4, 65 | "metadata": {}, 66 | "output_type": "execute_result" 67 | } 68 | ], 69 | "source": [ 70 | "exe = fluid.Executor(fluid.CPUPlace())\n", 71 | "exe.run(fluid.default_startup_program())" 72 | ] 73 | }, 74 | { 75 | "cell_type": "code", 76 | "execution_count": 5, 77 | "metadata": { 78 | "ExecuteTime": { 79 | "end_time": "2018-08-03T02:59:34.021923Z", 80 | "start_time": "2018-08-03T02:59:33.863882Z" 81 | } 82 | }, 83 | "outputs": [ 84 | { 85 | "data": { 86 | "text/plain": [ 87 | "[array([[0., 0., 2., 3., 4., 5., 6., 7., 8., 9.]], dtype=float32)]" 88 | ] 89 | }, 90 | "execution_count": 5, 91 | "metadata": {}, 92 | "output_type": "execute_result" 93 | } 94 | ], 95 | "source": [ 96 | "exe.run(fetch_list=[y], feed={\n", 97 | " 'x': np.arange(10).reshape(1, 10).astype('float32')\n", 98 | "})" 99 | ] 100 | }, 101 | { 102 | "cell_type": "code", 103 | "execution_count": 6, 104 | "metadata": { 105 | "ExecuteTime": { 106 | "end_time": "2018-08-03T02:59:34.083474Z", 107 | "start_time": "2018-08-03T02:59:34.025926Z" 108 | } 109 | }, 110 | "outputs": [ 111 | { 112 | "data": { 113 | "text/plain": [ 114 | "[array([[0. , 0.7 , 1.4 , 2.1 , 2.8 , 3.5 ,\n", 115 | " 4.2 , 4.9 , 5.6 , 6.2999997]], dtype=float32)]" 116 | ] 117 | }, 118 | "execution_count": 6, 119 | "metadata": {}, 120 | "output_type": "execute_result" 121 | } 122 | ], 123 | "source": [ 124 | "exe.run(test_program, fetch_list=[y], feed={\n", 125 | " 'x': np.arange(10).reshape(1, 10).astype('float32')\n", 126 | "})" 127 | ] 128 | }, 129 | { 130 | "cell_type": "code", 131 | "execution_count": null, 132 | "metadata": {}, 133 | "outputs": [], 134 | "source": [] 135 | } 136 | ], 137 | "metadata": { 138 | "kernelspec": { 139 | "display_name": "Python 2", 140 | "language": "python", 141 | "name": "python2" 142 | }, 143 | "language_info": { 144 | "codemirror_mode": { 145 | "name": "ipython", 146 | "version": 2 147 | }, 148 | "file_extension": ".py", 149 | "mimetype": "text/x-python", 150 | "name": "python", 151 | "nbconvert_exporter": "python", 152 | "pygments_lexer": "ipython2", 153 | "version": "2.7.15" 154 | } 155 | }, 156 | "nbformat": 4, 157 | "nbformat_minor": 2 158 | } 159 | -------------------------------------------------------------------------------- /gan/dcgan.py: -------------------------------------------------------------------------------- 1 | #%% 2 | import nnabla as nn 3 | import nnabla.functions as F 4 | import nnabla.parametric_functions as PF 5 | import nnabla.solvers as S 6 | import numpy as np 7 | from keras.datasets import mnist 8 | import matplotlib.pyplot as plt 9 | import time 10 | import networkx as nx 11 | import graphviz as gv 12 | 13 | # from nnabla.contrib.context import extension_context 14 | # cuda_device_id = 0 15 | # ctx = extension_context('cuda.cudnn', device_id=cuda_device_id) 16 | # nn.set_default_context(ctx) 17 | 18 | #%% Config 19 | z_dim = 100 20 | epochs = 2 21 | lr = 0.001 22 | batch_size = 64 23 | 24 | #%% Data 25 | (train_x, train_y), (test_x, test_y) = mnist.load_data() 26 | train_x = train_x.astype('float32').reshape(train_x.shape[0], 1, 28, 28) / 255. 27 | idx = np.random.permutation(len(train_x)) 28 | train_x = train_x[idx] 29 | 30 | def mnist_next_batch(): 31 | idx = np.random.randint(0, len(train_x) // batch_size - 1) 32 | true_images = train_x[idx * batch_size:(idx + 1) * batch_size] 33 | return true_images 34 | 35 | #%% Discriminator and Generator 36 | def D(images, is_train=True): 37 | with nn.parameter_scope('D'): 38 | with nn.parameter_scope('conv1'): 39 | conv1 = PF.convolution(images, 32, (3, 3), pad=(3, 3), stride=(2, 2), with_bias=False) 40 | conv1 = PF.batch_normalization(conv1, batch_stat=is_train) 41 | conv1 = F.leaky_relu(conv1) 42 | with nn.parameter_scope('conv2'): 43 | conv2 = PF.convolution(conv1, 64, (3, 3), pad=(1, 1), stride=(2, 2), with_bias=False) 44 | conv2 = PF.batch_normalization(conv2, batch_stat=is_train) 45 | conv2 = F.leaky_relu(conv2) 46 | with nn.parameter_scope('conv3'): 47 | conv3 = PF.convolution(conv2, 128, (3, 3), pad=(1, 1), stride=(2,2), with_bias=False) 48 | conv3 = PF.batch_normalization(conv3, batch_stat=is_train) 49 | conv3 = F.leaky_relu(conv3) 50 | with nn.parameter_scope('conv4'): 51 | conv4 = PF.convolution(conv3, 256, (3, 3), pad=(1, 1), stride=(1,1), with_bias=False) 52 | conv4 = PF.batch_normalization(conv4, batch_stat=is_train) 53 | with nn.parameter_scope('output'): 54 | output = PF.affine(conv4, 1) 55 | output = F.sigmoid(output) 56 | return output 57 | 58 | def G(z, is_train=True): 59 | z = F.reshape(z, [batch_size, z_dim, 1, 1]) 60 | with nn.parameter_scope('G'): 61 | with nn.parameter_scope('deconv1'): 62 | dc1 = PF.deconvolution(z, 256, (4, 4), with_bias=False) 63 | dc1 = PF.batch_normalization(dc1, batch_stat=is_train) 64 | dc1 = F.leaky_relu(dc1) 65 | with nn.parameter_scope('deconv2'): 66 | dc2 = PF.deconvolution(dc1, 128, (4, 4), pad=(1, 1), stride=(2, 2), with_bias=False) 67 | dc2 = PF.batch_normalization(dc2, batch_stat=is_train) 68 | dc2 = F.leaky_relu(dc2) 69 | with nn.parameter_scope('deconv3'): 70 | dc3 = PF.deconvolution(dc2, 64, (4, 4), pad=(1, 1), stride=(2, 2), with_bias=False) 71 | dc3 = PF.batch_normalization(dc3, batch_stat=is_train) 72 | dc3 = F.leaky_relu(dc3) 73 | with nn.parameter_scope('deconv4'): 74 | dc4 = PF.deconvolution(dc3, 32, (4, 4), pad=(3, 3), stride=(2,2), with_bias=False) 75 | dc4 = PF.batch_normalization(dc4, batch_stat=is_train) 76 | dc4 = F.leaky_relu(dc4) 77 | with nn.parameter_scope('output'): 78 | output = PF.convolution(dc4, 1, (3, 3), pad=(1,1)) 79 | output = F.sigmoid(output) 80 | return output 81 | 82 | nn.clear_parameters() 83 | 84 | #%% 85 | G_solver = S.Adam(0.0002, beta2=0.5) 86 | D_solver = S.Adam(0.0002, beta2=0.5) 87 | 88 | G(nn.Variable([batch_size, z_dim])) 89 | with nn.parameter_scope('G'): 90 | G_solver.set_parameters(nn.get_parameters()) 91 | 92 | D(nn.Variable([batch_size, 1, 28, 28])) 93 | with nn.parameter_scope('D'): 94 | D_solver.set_parameters(nn.get_parameters()) 95 | 96 | ones = nn.Variable.from_numpy_array(np.ones((batch_size, 1))) 97 | zeros = nn.Variable.from_numpy_array(np.zeros((batch_size, 1))) 98 | 99 | #%% 100 | def show_image(z=None): 101 | if not z: 102 | z = nn.Variable.from_numpy_array(np.random.randn(batch_size, z_dim)) 103 | fake_images = G(z, False) 104 | fake_images.forward() 105 | p = D(fake_images) 106 | p.forward() 107 | plt.show() 108 | r = np.sum((p.d > 0.5).astype('int8').reshape((batch_size,))) / batch_size 109 | 110 | fig = plt.figure(figsize=(5, 5)) 111 | fig.suptitle(str(r)) 112 | gs = plt.GridSpec(8, 8) 113 | gs.update(wspace=0.05, hspace=0.05) 114 | 115 | plt.title(str(r)) 116 | for i, image in enumerate(fake_images.d): 117 | ax = plt.subplot(gs[i]) 118 | plt.axis('off') 119 | ax.set_xticklabels([]) 120 | ax.set_yticklabels([]) 121 | ax.set_aspect('equal') 122 | plt.imshow(image.reshape(28, 28), cmap='Greys_r') 123 | plt.show() 124 | 125 | #show_image() 126 | 127 | #%% 128 | for i in range(10000): 129 | ## Fake image 130 | z = nn.Variable.from_numpy_array(np.random.randn(batch_size, z_dim)) 131 | fake_images = G(z) 132 | predict = D(fake_images) 133 | fake_loss = F.mean(F.binary_cross_entropy(predict, zeros)) 134 | 135 | D_solver.zero_grad() 136 | fake_loss.forward() 137 | fake_loss.backward(clear_buffer=True) 138 | D_solver.update() 139 | 140 | ## Real image 141 | true_images = nn.Variable.from_numpy_array(mnist_next_batch()) 142 | predict = D(true_images) 143 | real_loss = F.mean(F.binary_cross_entropy(predict, ones)) 144 | 145 | D_solver.zero_grad() 146 | real_loss.forward() 147 | real_loss.backward(clear_buffer=True) 148 | D_solver.update() 149 | 150 | # # G 151 | z = nn.Variable.from_numpy_array(np.random.randn(batch_size, z_dim)) 152 | fake_images = G(z) 153 | predict = D(fake_images) 154 | g_loss = F.mean(F.binary_cross_entropy(predict, ones)) 155 | 156 | G_solver.zero_grad() 157 | g_loss.forward() 158 | g_loss.backward(clear_buffer=True) 159 | G_solver.update() 160 | 161 | if i % 100 == 0: 162 | show_image() 163 | print('GAN train loss', i, g_loss.d, real_loss.d, fake_loss.d) 164 | if i % 500 == 0: 165 | nn.save_parameters('params_iter' + str(i) + '.h5') 166 | 167 | #%% Interpolation 168 | def interp(a, b, step): 169 | result = [] 170 | delta = (b - a) / (step - 1) 171 | 172 | for i in range(step): 173 | result.append(a + i * delta) 174 | return np.concatenate(result) 175 | 176 | 177 | zs = np.random.randn(4, z_dim) 178 | z1, z2, z3, z4 = zs[0], zs[1], zs[2], zs[3] 179 | 180 | zs = np.concatenate([z1, z2, z3, z4]) 181 | show_image(nn.Variable.from_numpy_array(np.resize(zs, (batch_size, z_dim)))) 182 | 183 | z = interp(interp(z1, z2, 8), interp(z3, z4, 8), 8) 184 | 185 | show_image(nn.Variable.from_numpy_array(z)) 186 | -------------------------------------------------------------------------------- /paddle/inference_model.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": { 7 | "ExecuteTime": { 8 | "end_time": "2018-07-25T07:15:39.676325Z", 9 | "start_time": "2018-07-25T07:15:38.910266Z" 10 | } 11 | }, 12 | "outputs": [], 13 | "source": [ 14 | "import paddle.fluid as fluid\n", 15 | "import numpy as np\n", 16 | "\n", 17 | "from paddle.fluid.debugger import draw_block_graphviz" 18 | ] 19 | }, 20 | { 21 | "cell_type": "code", 22 | "execution_count": 2, 23 | "metadata": { 24 | "ExecuteTime": { 25 | "end_time": "2018-07-25T07:15:39.685358Z", 26 | "start_time": "2018-07-25T07:15:39.680335Z" 27 | } 28 | }, 29 | "outputs": [], 30 | "source": [ 31 | "# 一个简单的网络\n", 32 | "def network(inp):\n", 33 | " y = fluid.layers.fc(inp, size=1, act=None)\n", 34 | " return y" 35 | ] 36 | }, 37 | { 38 | "cell_type": "code", 39 | "execution_count": 3, 40 | "metadata": { 41 | "ExecuteTime": { 42 | "end_time": "2018-07-25T07:15:39.763637Z", 43 | "start_time": "2018-07-25T07:15:39.688932Z" 44 | } 45 | }, 46 | "outputs": [], 47 | "source": [ 48 | "# 训练数据, y = 2x + 3\n", 49 | "data_x = np.arange(128).reshape(128, 1).astype('float32')\n", 50 | "data_y = (data_x * 2 + 3).reshape(128, 1)\n", 51 | "test_x =np.array([1024, 600, 666]).reshape(3, 1).astype('float32')" 52 | ] 53 | }, 54 | { 55 | "cell_type": "code", 56 | "execution_count": 4, 57 | "metadata": { 58 | "ExecuteTime": { 59 | "end_time": "2018-07-25T07:15:41.011601Z", 60 | "start_time": "2018-07-25T07:15:39.767939Z" 61 | } 62 | }, 63 | "outputs": [ 64 | { 65 | "name": "stdout", 66 | "output_type": "stream", 67 | "text": [ 68 | "Epoch 0 loss 27859.640625\n", 69 | "Epoch 100 loss 0.883998\n", 70 | "Epoch 200 loss 0.115282\n", 71 | "Epoch 300 loss 0.094322\n", 72 | "Epoch 400 loss 0.073891\n", 73 | "Epoch 500 loss 0.055575\n", 74 | "Epoch 600 loss 0.040188\n", 75 | "Epoch 700 loss 0.027956\n", 76 | "Epoch 800 loss 0.018710\n", 77 | "Epoch 900 loss 0.012042\n", 78 | "[array([[2052.9038],\n", 79 | " [1204.0444],\n", 80 | " [1336.1782]], dtype=float32)]\n" 81 | ] 82 | } 83 | ], 84 | "source": [ 85 | "main = fluid.Program()\n", 86 | "startup = fluid.Program()\n", 87 | "scope = fluid.Scope()\n", 88 | "\n", 89 | "with fluid.unique_name.guard():\n", 90 | " with fluid.scope_guard(scope):\n", 91 | " with fluid.program_guard(main ,startup):\n", 92 | " \n", 93 | " # 预测网络\n", 94 | " x = fluid.layers.data('x', [1])\n", 95 | " predict_y = network(x)\n", 96 | " \n", 97 | " inference_program = main.clone(for_test=True)\n", 98 | " \n", 99 | " # 损失\n", 100 | " y = fluid.layers.data('y', [1])\n", 101 | " loss = fluid.layers.square_error_cost(input=predict_y, label=y)\n", 102 | " loss = fluid.layers.mean(loss)\n", 103 | " \n", 104 | " # 优化器\n", 105 | " opt = fluid.optimizer.Adam(learning_rate=0.1)\n", 106 | " opt.minimize(loss)\n", 107 | "\n", 108 | " # 初始化变量\n", 109 | " exe = fluid.executor.Executor(fluid.CPUPlace())\n", 110 | " exe.run(startup)\n", 111 | "\n", 112 | " # 训练\n", 113 | " for i in range(1000):\n", 114 | " result = exe.run(fetch_list=[loss], feed={\n", 115 | " 'x': data_x,\n", 116 | " 'y': data_y\n", 117 | " })\n", 118 | " if i % 100 == 0:\n", 119 | " print(\"Epoch %i loss %f\" %(i, result[0][0]))\n", 120 | " \n", 121 | " # 预测\n", 122 | " result = exe.run(inference_program, fetch_list=[predict_y], feed={\n", 123 | " 'x': test_x\n", 124 | " })\n", 125 | " print(result)\n", 126 | " \n", 127 | " fluid.io.save_inference_model(\"models/linear\", \n", 128 | " feeded_var_names=['x'], \n", 129 | " target_vars=[predict_y],\n", 130 | " executor=exe)" 131 | ] 132 | }, 133 | { 134 | "cell_type": "code", 135 | "execution_count": 5, 136 | "metadata": { 137 | "ExecuteTime": { 138 | "end_time": "2018-07-25T07:15:41.021366Z", 139 | "start_time": "2018-07-25T07:15:41.015381Z" 140 | } 141 | }, 142 | "outputs": [], 143 | "source": [ 144 | "exe = fluid.executor.Executor(fluid.CPUPlace())\n", 145 | "inference_program, feed_names, fetch_list = fluid.io.load_inference_model(\"models/linear\", exe)" 146 | ] 147 | }, 148 | { 149 | "cell_type": "code", 150 | "execution_count": 6, 151 | "metadata": { 152 | "ExecuteTime": { 153 | "end_time": "2018-07-25T07:15:41.148508Z", 154 | "start_time": "2018-07-25T07:15:41.025422Z" 155 | } 156 | }, 157 | "outputs": [ 158 | { 159 | "name": "stderr", 160 | "output_type": "stream", 161 | "text": [ 162 | "WARNING:root:write block debug graph to infer.pdf\n" 163 | ] 164 | } 165 | ], 166 | "source": [ 167 | "draw_block_graphviz(inference_program.blocks[0], path=\"infer.pdf\")" 168 | ] 169 | }, 170 | { 171 | "cell_type": "code", 172 | "execution_count": 7, 173 | "metadata": { 174 | "ExecuteTime": { 175 | "end_time": "2018-07-25T07:15:41.184151Z", 176 | "start_time": "2018-07-25T07:15:41.153438Z" 177 | } 178 | }, 179 | "outputs": [ 180 | { 181 | "data": { 182 | "text/plain": [ 183 | "[array([[2052.9038],\n", 184 | " [1204.0444],\n", 185 | " [1336.1782]], dtype=float32)]" 186 | ] 187 | }, 188 | "execution_count": 7, 189 | "metadata": {}, 190 | "output_type": "execute_result" 191 | } 192 | ], 193 | "source": [ 194 | "exe.run(inference_program, fetch_list=fetch_list, feed={\n", 195 | " 'x': test_x\n", 196 | "})" 197 | ] 198 | }, 199 | { 200 | "cell_type": "code", 201 | "execution_count": null, 202 | "metadata": {}, 203 | "outputs": [], 204 | "source": [] 205 | } 206 | ], 207 | "metadata": { 208 | "kernelspec": { 209 | "display_name": "Python 2", 210 | "language": "python", 211 | "name": "python2" 212 | }, 213 | "language_info": { 214 | "codemirror_mode": { 215 | "name": "ipython", 216 | "version": 2 217 | }, 218 | "file_extension": ".py", 219 | "mimetype": "text/x-python", 220 | "name": "python", 221 | "nbconvert_exporter": "python", 222 | "pygments_lexer": "ipython2", 223 | "version": "2.7.15" 224 | } 225 | }, 226 | "nbformat": 4, 227 | "nbformat_minor": 2 228 | } 229 | -------------------------------------------------------------------------------- /text-classification/text-cnn.py: -------------------------------------------------------------------------------- 1 | #%% Import 2 | import numpy as np 3 | import pandas as pd 4 | import nnabla as nn 5 | import nnabla.functions as F 6 | import nnabla.parametric_functions as PF 7 | import nnabla.solvers as S 8 | from sklearn.preprocessing import LabelEncoder 9 | from sklearn.metrics import classification_report, confusion_matrix 10 | #import matplotlib.pyplot as plt 11 | import os 12 | import math 13 | import sys 14 | import pickle 15 | 16 | from nnabla.contrib.context import extension_context 17 | cuda_device_id = 0 18 | ctx = extension_context('cuda.cudnn', device_id=cuda_device_id) 19 | nn.set_default_context(ctx) 20 | 21 | #%% Config 22 | text_len = 600 23 | batch_size = 256 24 | epochs = 20 25 | learning_rate = 0.001 26 | 27 | #%% Read data 28 | def read_file(path): 29 | with open(path, encoding="utf-8") as file: 30 | data = [line.split("\t") for line in file.readlines()] 31 | return pd.DataFrame(data, columns=["category", "text"]) 32 | 33 | train_data = read_file("./data/cnews.train.txt") 34 | validate_data = read_file("./data/cnews.val.txt") 35 | test_data = read_file("./data/cnews.test.txt") 36 | 37 | #%% Transform words and categories to integer 38 | class Encoder: 39 | def __init__(self, text_len=600): 40 | self.text_len = text_len 41 | self.wtoi = {} 42 | self.itow = {} 43 | self.label_encoder = LabelEncoder() 44 | 45 | def fit(self, X): 46 | for _, words in X['text'].items(): 47 | words = list(str(words)) 48 | for w in words: 49 | if w not in self.wtoi: 50 | i = len(self.wtoi) 51 | self.wtoi[w] = i 52 | self.itow[i] = w 53 | self.label_encoder.fit(X['category']) 54 | 55 | def transform(self, X): 56 | text_dataset = [] 57 | 58 | for _, words in X['text'].items(): 59 | row = [] 60 | for w in words: 61 | if w not in self.wtoi: 62 | i = 0 63 | else: 64 | i = self.wtoi[w] 65 | row.append(i) 66 | if len(row) > self.text_len: 67 | row[:self.text_len] 68 | text_dataset.append(np.resize(np.array(row), (self.text_len,))) # padding不是填0 69 | 70 | texts = np.stack(text_dataset) 71 | labels = self.label_encoder.transform(X['category']) 72 | return texts, labels 73 | 74 | encoder = Encoder() 75 | train_data = train_data.sample(frac=1) 76 | encoder.fit(train_data) 77 | train_data, train_labels = encoder.transform(train_data) 78 | validate_data, validate_labels = encoder.transform(validate_data) 79 | test_data, test_labels = encoder.transform(test_data) 80 | 81 | #%% Define the network 82 | def cnn(batch_size, vocab_size, text_len, classes, features=128, train=True): 83 | text = nn.Variable([batch_size, text_len]) 84 | 85 | with nn.parameter_scope("text_embed"): 86 | embed = PF.embed(text, n_inputs=vocab_size, n_features=features) 87 | print("embed", embed.shape) 88 | 89 | embed = F.reshape(embed, (batch_size, 1, text_len, features)) 90 | print("embed", embed.shape) 91 | 92 | combined = None 93 | for n in range(2, 6): # 2 - 5 gram 94 | with nn.parameter_scope(str(n) + "_gram"): 95 | with nn.parameter_scope("conv"): 96 | conv = PF.convolution(embed, 128, kernel=(n, features)) 97 | conv = F.relu(conv) 98 | with nn.parameter_scope("pool"): 99 | pool = F.max_pooling(conv, kernel=(conv.shape[2], 1)) 100 | if not combined: 101 | combined = F.identity(pool) 102 | else: 103 | combined = F.concatenate(combined, pool) 104 | 105 | if train: 106 | combined = F.dropout(combined, 0.5) 107 | 108 | with nn.parameter_scope("output"): 109 | y = PF.affine(combined, classes) 110 | 111 | t = nn.Variable([batch_size, 1]) 112 | 113 | _loss = F.softmax_cross_entropy(y, t) 114 | loss = F.reduce_mean(_loss) 115 | 116 | return text, y, loss, t 117 | 118 | def validate(data, labels): 119 | iterations = math.ceil(len(data) / batch_size) 120 | predicts = [] 121 | truths = [] 122 | 123 | X, y, _, _ = cnn(batch_size, len(encoder.wtoi), encoder.text_len, len(np.unique(train_labels)), train=False) 124 | 125 | for i in range(iterations): 126 | d = data[i * batch_size : (i + 1) * batch_size] 127 | l = labels[i * batch_size : (i + 1) * batch_size] 128 | 129 | len_d = len(d) 130 | 131 | if len_d < batch_size: 132 | d = np.resize(d, (batch_size, text_len)) 133 | 134 | X.d = d 135 | y.forward() 136 | predict = y.d.argmax(axis=1) 137 | 138 | if len_d < batch_size: 139 | predict = predict[:len_d] 140 | 141 | predicts.append(predict) 142 | truths.append(l) 143 | 144 | predicts = np.concatenate(predicts) 145 | truths = np.concatenate(truths) 146 | print(predicts) 147 | print(truths) 148 | 149 | print(classification_report(truths, predicts, digits=4)) 150 | 151 | return predicts, truths 152 | 153 | #% Train: solver 154 | nn.clear_parameters() 155 | cnn(batch_size, len(encoder.wtoi), encoder.text_len, len(np.unique(train_labels))) 156 | solver = S.Adam(learning_rate) 157 | solver.set_parameters(nn.get_parameters()) 158 | nn.save_parameters("text-cnn.h5") 159 | 160 | #% Train: data collection 161 | iterations = math.ceil(len(train_data) / batch_size) 162 | all_loss = [] 163 | all_average_loss = [] 164 | min_loss = 10 165 | 166 | #%% Train: loop 167 | for epoch in range(epochs): 168 | X, y, loss, t = cnn(batch_size, len(encoder.wtoi), encoder.text_len, len(np.unique(train_labels))) 169 | total_loss = 0 170 | 171 | index = np.random.permutation(len(train_data)) 172 | train_data = train_data[index] 173 | train_labels = train_labels[index] 174 | 175 | for i in range(iterations): 176 | d = train_data[i * batch_size : (i + 1) * batch_size] 177 | l = train_labels[i * batch_size : (i + 1) * batch_size] 178 | if len(d) < batch_size: # some data is skipped 179 | continue 180 | X.d = d 181 | t.d = l.reshape((batch_size, 1)) 182 | loss.forward() 183 | solver.zero_grad() 184 | loss.backward() 185 | solver.weight_decay(1e-5) 186 | solver.update() 187 | all_loss.append(loss.d.max()) 188 | total_loss += loss.d.max() 189 | 190 | if i % 20 == 0: 191 | print("Iteration loss", epoch, i, loss.d.max()) 192 | 193 | average_loss = total_loss / iterations 194 | all_average_loss.append(average_loss) 195 | 196 | print("Epoch loss", epoch, average_loss) 197 | 198 | print("Validate") 199 | validate(validate_data, validate_labels) 200 | print("Train") 201 | validate(train_data, train_labels) 202 | 203 | if average_loss < min_loss: 204 | nn.save_parameters("./params_epoch_" + str(epoch) + ".h5") 205 | min_loss = average_loss 206 | 207 | #%% 208 | predict, truth = validate(validate_data, validate_labels) 209 | predict, truth = validate(test_data, test_labels) 210 | confusion_matrix(truth, predict) 211 | -------------------------------------------------------------------------------- /text-classification/fast-text.py: -------------------------------------------------------------------------------- 1 | #%% Import 2 | import numpy as np 3 | import pandas as pd 4 | import nnabla as nn 5 | import nnabla.functions as F 6 | import nnabla.parametric_functions as PF 7 | import nnabla.solvers as S 8 | from encoder import encoder, train_data, train_labels, validate_data, validate_labels, read_file 9 | from sklearn.metrics import classification_report, confusion_matrix 10 | import matplotlib.pyplot as plt 11 | import pickle 12 | import math 13 | import sys 14 | import os 15 | 16 | #%% 17 | batch_size = 1024 18 | epochs = 20 19 | learning_rate = 0.01 20 | param_file = "fast-text.h5" 21 | features = 32 22 | 23 | text_len = encoder.text_len 24 | classes = len(np.unique(train_labels)) 25 | vocab_size = len(encoder.wtoi) 26 | 27 | #%% Define the network 28 | def fast_text(batch_size, vocab_size, text_len, classes, features, train=True): 29 | text = nn.Variable([batch_size, text_len]) 30 | 31 | with nn.parameter_scope("text_embed"): 32 | embed = PF.embed(text, n_inputs=vocab_size, n_features=features) 33 | 34 | avg = F.mean(embed, axis=1) 35 | 36 | with nn.parameter_scope("output"): 37 | y = PF.affine(avg, classes) 38 | 39 | t = nn.Variable([batch_size, 1]) 40 | 41 | _loss = F.softmax_cross_entropy(y, t) 42 | loss = F.reduce_mean(_loss) 43 | 44 | return text, y, loss, t 45 | 46 | def validate(data, labels): 47 | iterations = math.ceil(len(data) / batch_size) 48 | predicts = [] 49 | truths = [] 50 | 51 | X, y, _, _ = net(train=False) 52 | 53 | for i in range(iterations): 54 | d = data[i * batch_size : (i + 1) * batch_size] 55 | l = labels[i * batch_size : (i + 1) * batch_size] 56 | 57 | len_d = len(d) 58 | 59 | if len_d < batch_size: 60 | d = np.resize(d, (batch_size, text_len)) 61 | 62 | X.d = d 63 | y.forward() 64 | predict = y.d.argmax(axis=1) 65 | 66 | if len_d < batch_size: 67 | predict = predict[:len_d] 68 | 69 | predicts.append(predict) 70 | truths.append(l) 71 | 72 | predicts = np.concatenate(predicts) 73 | truths = np.concatenate(truths) 74 | 75 | print(classification_report(truths, predicts)) 76 | 77 | return predicts, truths 78 | 79 | 80 | def net(train=True): 81 | return fast_text(batch_size, vocab_size, text_len, classes, features, train=True) 82 | 83 | #% Train: solver 84 | nn.clear_parameters() 85 | net(train=True) 86 | solver = S.Adam(learning_rate) 87 | solver.set_parameters(nn.get_parameters()) 88 | nn.save_parameters(param_file) 89 | 90 | #% Train: data collection 91 | iterations = math.ceil(len(train_data) / batch_size) 92 | all_loss = [] 93 | all_average_loss = [] 94 | min_loss = 10 95 | 96 | #%% Train: loop 97 | for epoch in range(epochs): 98 | X, y, loss, t = net(train=True) 99 | total_loss = 0 100 | 101 | index = np.random.permutation(len(train_data)) 102 | train_data = train_data[index] 103 | train_labels = train_labels[index] 104 | 105 | for i in range(iterations): 106 | d = train_data[i * batch_size : (i + 1) * batch_size] 107 | l = train_labels[i * batch_size : (i + 1) * batch_size] 108 | if len(d) < batch_size: # some data is skipped 109 | continue 110 | X.d = d 111 | t.d = l.reshape((batch_size, 1)) 112 | loss.forward() 113 | solver.zero_grad() 114 | loss.backward() 115 | solver.weight_decay(1e-5) 116 | solver.update() 117 | all_loss.append(loss.d.max()) 118 | total_loss += loss.d.max() 119 | 120 | if i % 10 == 0: 121 | print("Iteration loss", epoch, i, i / iterations, loss.d.max()) 122 | 123 | average_loss = total_loss / iterations 124 | all_average_loss.append(average_loss) 125 | 126 | print("Epoch loss", epoch, average_loss) 127 | print("Train:") 128 | validate(train_data, train_labels) 129 | print("Validate:") 130 | validate(validate_data, validate_labels) 131 | 132 | nn.save_parameters("./params_epoch_" + str(epoch) + ".h5") 133 | if average_loss < min_loss: 134 | nn.save_parameters(param_file) 135 | min_loss = average_loss 136 | 137 | #%% Plot 138 | fig = plt.gcf() 139 | fig.set_size_inches(10, 5, forward=True) 140 | plt.plot(all_loss, 'k-') 141 | plt.title('Cross Entropy Loss per Batch') 142 | plt.xlabel('Batch') 143 | plt.yscale('log') 144 | plt.ylabel('Cross Entropy Loss') 145 | plt.show() 146 | 147 | fig = plt.gcf() 148 | fig.set_size_inches(10, 5, forward=True) 149 | plt.plot(all_average_loss, 'k-') 150 | plt.title('Average Cross Entropy Loss per Epoch') 151 | plt.yscale('log') 152 | plt.xlabel('Epoch') 153 | plt.ylabel('Average Cross Entropy Loss') 154 | plt.show() 155 | 156 | #%% Validate 157 | predict, truth = validate(validate_data, validate_labels) 158 | print(confusion_matrix(truth, predict)) 159 | predict, truth = validate(train_data, train_labels) 160 | print(confusion_matrix(truth, predict)) 161 | 162 | # precision recall f1-score support 163 | 164 | # 0 0.99 0.99 0.99 500 165 | # 1 0.97 0.88 0.92 500 166 | # 2 0.97 0.72 0.83 500 167 | # 3 0.81 0.93 0.86 500 168 | # 4 0.93 0.89 0.91 500 169 | # 5 0.94 0.98 0.96 500 170 | # 6 0.92 0.94 0.93 500 171 | # 7 0.92 0.97 0.94 500 172 | # 8 0.92 0.99 0.95 500 173 | # 9 0.95 0.97 0.96 500 174 | 175 | # avg / total 0.93 0.93 0.93 5000 176 | 177 | # [[496 0 0 0 4 0 0 0 0 0] 178 | # [ 4 442 1 0 4 10 0 18 18 3] 179 | # [ 1 5 362 81 6 14 11 1 11 8] 180 | # [ 0 2 8 465 6 2 9 2 0 6] 181 | # [ 0 1 2 2 446 0 17 21 8 3] 182 | # [ 1 4 0 0 2 492 0 0 1 0] 183 | # [ 0 3 0 14 5 0 472 1 2 3] 184 | # [ 0 1 1 1 3 3 0 486 5 0] 185 | # [ 0 0 1 0 1 1 0 2 494 1] 186 | # [ 0 0 0 14 1 0 2 0 0 483]] 187 | 188 | # precision recall f1-score support 189 | 190 | # 0 1.00 0.99 1.00 5000 191 | # 1 0.98 0.99 0.99 5000 192 | # 2 0.98 0.97 0.98 5000 193 | # 3 0.94 0.96 0.95 5000 194 | # 4 0.95 0.96 0.96 5000 195 | # 5 0.98 0.99 0.98 5000 196 | # 6 0.96 0.96 0.96 5000 197 | # 7 0.99 0.98 0.99 5000 198 | # 8 0.96 0.97 0.96 5000 199 | # 9 0.97 0.94 0.95 5000 200 | 201 | # avg / total 0.97 0.97 0.97 50000 202 | 203 | # [[4973 3 1 2 8 2 1 3 4 3] 204 | # [ 2 4947 3 3 6 21 6 8 2 2] 205 | # [ 0 14 4868 66 15 23 7 2 3 2] 206 | # [ 0 5 29 4806 16 4 61 1 7 71] 207 | # [ 4 10 6 15 4809 20 20 10 86 20] 208 | # [ 2 20 11 7 16 4926 4 5 7 2] 209 | # [ 1 5 3 77 29 0 4820 1 41 23] 210 | # [ 4 15 3 6 19 9 4 4924 14 2] 211 | # [ 3 11 2 16 62 5 42 14 4832 13] 212 | # [ 2 7 17 136 62 10 48 1 24 4693]] 213 | -------------------------------------------------------------------------------- /paddle/variable.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": { 7 | "ExecuteTime": { 8 | "end_time": "2018-07-25T07:02:43.392366Z", 9 | "start_time": "2018-07-25T07:02:42.637124Z" 10 | } 11 | }, 12 | "outputs": [], 13 | "source": [ 14 | "import paddle.fluid as fluid\n", 15 | "import numpy as np\n", 16 | "from paddle.fluid.debugger import draw_block_graphviz" 17 | ] 18 | }, 19 | { 20 | "cell_type": "code", 21 | "execution_count": 2, 22 | "metadata": { 23 | "ExecuteTime": { 24 | "end_time": "2018-07-25T07:02:43.411320Z", 25 | "start_time": "2018-07-25T07:02:43.396362Z" 26 | } 27 | }, 28 | "outputs": [ 29 | { 30 | "data": { 31 | "text/plain": [ 32 | "array([[0., 1., 2.],\n", 33 | " [3., 4., 5.],\n", 34 | " [6., 7., 8.]], dtype=float32)" 35 | ] 36 | }, 37 | "execution_count": 2, 38 | "metadata": {}, 39 | "output_type": "execute_result" 40 | } 41 | ], 42 | "source": [ 43 | "test_data = np.arange(9).reshape(3,3).astype('float32')\n", 44 | "test_data" 45 | ] 46 | }, 47 | { 48 | "cell_type": "markdown", 49 | "metadata": {}, 50 | "source": [ 51 | "## Create Variable in Program" 52 | ] 53 | }, 54 | { 55 | "cell_type": "code", 56 | "execution_count": 3, 57 | "metadata": { 58 | "ExecuteTime": { 59 | "end_time": "2018-07-25T07:02:43.471779Z", 60 | "start_time": "2018-07-25T07:02:43.415303Z" 61 | } 62 | }, 63 | "outputs": [], 64 | "source": [ 65 | "main = fluid.Program()\n", 66 | "block = main.current_block()" 67 | ] 68 | }, 69 | { 70 | "cell_type": "code", 71 | "execution_count": 4, 72 | "metadata": { 73 | "ExecuteTime": { 74 | "end_time": "2018-07-25T07:02:43.533157Z", 75 | "start_time": "2018-07-25T07:02:43.476291Z" 76 | } 77 | }, 78 | "outputs": [ 79 | { 80 | "data": { 81 | "text/plain": [ 82 | "(name: \"x\"\n", 83 | " type {\n", 84 | " type: LOD_TENSOR\n", 85 | " lod_tensor {\n", 86 | " tensor {\n", 87 | " data_type: FP32\n", 88 | " dims: 3\n", 89 | " dims: 3\n", 90 | " }\n", 91 | " }\n", 92 | " }, paddle.fluid.framework.Variable)" 93 | ] 94 | }, 95 | "execution_count": 4, 96 | "metadata": {}, 97 | "output_type": "execute_result" 98 | } 99 | ], 100 | "source": [ 101 | "x = block.create_var(name=\"x\", shape=[3,3], dtype='float32')\n", 102 | "x, type(x)" 103 | ] 104 | }, 105 | { 106 | "cell_type": "code", 107 | "execution_count": 5, 108 | "metadata": { 109 | "ExecuteTime": { 110 | "end_time": "2018-07-25T07:02:43.595669Z", 111 | "start_time": "2018-07-25T07:02:43.536060Z" 112 | } 113 | }, 114 | "outputs": [ 115 | { 116 | "data": { 117 | "text/plain": [ 118 | "(name: \"mean_0.tmp_0\"\n", 119 | " type {\n", 120 | " type: LOD_TENSOR\n", 121 | " lod_tensor {\n", 122 | " tensor {\n", 123 | " data_type: FP32\n", 124 | " dims: 1\n", 125 | " }\n", 126 | " }\n", 127 | " }\n", 128 | " persistable: false, paddle.fluid.framework.Variable)" 129 | ] 130 | }, 131 | "execution_count": 5, 132 | "metadata": {}, 133 | "output_type": "execute_result" 134 | } 135 | ], 136 | "source": [ 137 | "with fluid.program_guard(main):\n", 138 | " y = fluid.layers.ops.mean(x) # Layer会向当前Main program和Starup program插入op\n", 139 | "y, type(y)" 140 | ] 141 | }, 142 | { 143 | "cell_type": "code", 144 | "execution_count": 6, 145 | "metadata": { 146 | "ExecuteTime": { 147 | "end_time": "2018-07-25T07:02:43.647869Z", 148 | "start_time": "2018-07-25T07:02:43.602447Z" 149 | } 150 | }, 151 | "outputs": [], 152 | "source": [ 153 | "exe = fluid.executor.Executor(fluid.CPUPlace())\n", 154 | "# 这里不需要startup program,因为全部变量都创建了" 155 | ] 156 | }, 157 | { 158 | "cell_type": "code", 159 | "execution_count": 7, 160 | "metadata": { 161 | "ExecuteTime": { 162 | "end_time": "2018-07-25T07:02:43.771652Z", 163 | "start_time": "2018-07-25T07:02:43.655067Z" 164 | } 165 | }, 166 | "outputs": [ 167 | { 168 | "data": { 169 | "text/plain": [ 170 | "[array([4.], dtype=float32)]" 171 | ] 172 | }, 173 | "execution_count": 7, 174 | "metadata": {}, 175 | "output_type": "execute_result" 176 | } 177 | ], 178 | "source": [ 179 | "exe.run(program=main, fetch_list=[y], feed={x.name: test_data})" 180 | ] 181 | }, 182 | { 183 | "cell_type": "code", 184 | "execution_count": 8, 185 | "metadata": { 186 | "ExecuteTime": { 187 | "end_time": "2018-07-25T07:02:43.864169Z", 188 | "start_time": "2018-07-25T07:02:43.775486Z" 189 | } 190 | }, 191 | "outputs": [ 192 | { 193 | "name": "stderr", 194 | "output_type": "stream", 195 | "text": [ 196 | "WARNING:root:write block debug graph to out.pdf\n" 197 | ] 198 | } 199 | ], 200 | "source": [ 201 | "draw_block_graphviz(y.block, path=\"out.pdf\")" 202 | ] 203 | }, 204 | { 205 | "cell_type": "markdown", 206 | "metadata": { 207 | "ExecuteTime": { 208 | "end_time": "2018-07-16T02:13:50.249146Z", 209 | "start_time": "2018-07-16T02:13:50.245393Z" 210 | } 211 | }, 212 | "source": [ 213 | "## Create Variable in global block" 214 | ] 215 | }, 216 | { 217 | "cell_type": "code", 218 | "execution_count": 9, 219 | "metadata": { 220 | "ExecuteTime": { 221 | "end_time": "2018-07-25T07:02:43.923834Z", 222 | "start_time": "2018-07-25T07:02:43.869177Z" 223 | } 224 | }, 225 | "outputs": [], 226 | "source": [ 227 | "x = fluid.layers.create_global_var(shape=[3,3], value=1.0, dtype='float32')" 228 | ] 229 | }, 230 | { 231 | "cell_type": "code", 232 | "execution_count": 10, 233 | "metadata": { 234 | "ExecuteTime": { 235 | "end_time": "2018-07-25T07:02:44.004639Z", 236 | "start_time": "2018-07-25T07:02:43.933673Z" 237 | } 238 | }, 239 | "outputs": [], 240 | "source": [ 241 | "y = fluid.layers.ops.mean(x)" 242 | ] 243 | }, 244 | { 245 | "cell_type": "code", 246 | "execution_count": 11, 247 | "metadata": { 248 | "ExecuteTime": { 249 | "end_time": "2018-07-25T07:02:44.054659Z", 250 | "start_time": "2018-07-25T07:02:44.008702Z" 251 | } 252 | }, 253 | "outputs": [], 254 | "source": [ 255 | "exe = fluid.executor.Executor(fluid.CPUPlace())" 256 | ] 257 | }, 258 | { 259 | "cell_type": "code", 260 | "execution_count": 12, 261 | "metadata": { 262 | "ExecuteTime": { 263 | "end_time": "2018-07-25T07:02:44.115695Z", 264 | "start_time": "2018-07-25T07:02:44.058901Z" 265 | } 266 | }, 267 | "outputs": [ 268 | { 269 | "data": { 270 | "text/plain": [ 271 | "[array([4.], dtype=float32)]" 272 | ] 273 | }, 274 | "execution_count": 12, 275 | "metadata": {}, 276 | "output_type": "execute_result" 277 | } 278 | ], 279 | "source": [ 280 | "exe.run(fetch_list=[y], feed={x.name: test_data})" 281 | ] 282 | } 283 | ], 284 | "metadata": { 285 | "kernelspec": { 286 | "display_name": "Python 2", 287 | "language": "python", 288 | "name": "python2" 289 | }, 290 | "language_info": { 291 | "codemirror_mode": { 292 | "name": "ipython", 293 | "version": 2 294 | }, 295 | "file_extension": ".py", 296 | "mimetype": "text/x-python", 297 | "name": "python", 298 | "nbconvert_exporter": "python", 299 | "pygments_lexer": "ipython2", 300 | "version": "2.7.15" 301 | } 302 | }, 303 | "nbformat": 4, 304 | "nbformat_minor": 2 305 | } 306 | -------------------------------------------------------------------------------- /paddle/initializer.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": { 7 | "ExecuteTime": { 8 | "end_time": "2018-07-25T07:11:56.992459Z", 9 | "start_time": "2018-07-25T07:11:56.052048Z" 10 | } 11 | }, 12 | "outputs": [], 13 | "source": [ 14 | "import paddle.fluid as fluid\n", 15 | "import paddle\n", 16 | "import matplotlib.pyplot as plt\n", 17 | "import numpy as np\n", 18 | "from paddle.fluid.debugger import draw_block_graphviz" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": 2, 24 | "metadata": { 25 | "ExecuteTime": { 26 | "end_time": "2018-07-25T07:11:57.675445Z", 27 | "start_time": "2018-07-25T07:11:57.648683Z" 28 | } 29 | }, 30 | "outputs": [], 31 | "source": [ 32 | "class NumpyInitializer(fluid.initializer.Initializer):\n", 33 | " \n", 34 | " def __init__(self, ndarray):\n", 35 | " super(NumpyInitializer, self).__init__()\n", 36 | " self._ndarray = ndarray.astype('float32')\n", 37 | "\n", 38 | " def __call__(self, var, block):\n", 39 | " values = [float(v) for v in self._ndarray.flat]\n", 40 | " op = block.append_op(\n", 41 | " type=\"assign_value\",\n", 42 | " outputs={\"Out\": [var]},\n", 43 | " attrs={\n", 44 | " \"shape\": var.shape,\n", 45 | " \"dtype\": int(var.dtype),\n", 46 | " \"fp32_values\": values\n", 47 | " })\n", 48 | " var.op = op\n", 49 | " return op" 50 | ] 51 | }, 52 | { 53 | "cell_type": "code", 54 | "execution_count": 3, 55 | "metadata": { 56 | "ExecuteTime": { 57 | "end_time": "2018-07-25T07:11:59.429370Z", 58 | "start_time": "2018-07-25T07:11:59.407280Z" 59 | }, 60 | "scrolled": false 61 | }, 62 | "outputs": [], 63 | "source": [ 64 | "w = np.arange(10).reshape(5, 2).astype('float32')\n", 65 | "b = np.arange(2).astype('float32')\n", 66 | "\n", 67 | "x = fluid.layers.data('x', [-1, 5], dtype='float32')\n", 68 | "y = fluid.layers.fc(x, size=2, param_attr=NumpyInitializer(w), bias_attr=NumpyInitializer(b))" 69 | ] 70 | }, 71 | { 72 | "cell_type": "code", 73 | "execution_count": 4, 74 | "metadata": { 75 | "ExecuteTime": { 76 | "end_time": "2018-07-25T07:12:00.194099Z", 77 | "start_time": "2018-07-25T07:12:00.168358Z" 78 | } 79 | }, 80 | "outputs": [ 81 | { 82 | "name": "stderr", 83 | "output_type": "stream", 84 | "text": [ 85 | "WARNING:root:write block debug graph to g.pdf\n" 86 | ] 87 | } 88 | ], 89 | "source": [ 90 | "draw_block_graphviz(y.block, path=\"g.pdf\")" 91 | ] 92 | }, 93 | { 94 | "cell_type": "code", 95 | "execution_count": 5, 96 | "metadata": { 97 | "ExecuteTime": { 98 | "end_time": "2018-07-25T07:12:02.253444Z", 99 | "start_time": "2018-07-25T07:12:02.160998Z" 100 | } 101 | }, 102 | "outputs": [ 103 | { 104 | "data": { 105 | "text/plain": [ 106 | "[array([[20., 26.]], dtype=float32)]" 107 | ] 108 | }, 109 | "execution_count": 5, 110 | "metadata": {}, 111 | "output_type": "execute_result" 112 | } 113 | ], 114 | "source": [ 115 | "exe = fluid.executor.Executor(fluid.CPUPlace())\n", 116 | "exe.run(fluid.default_startup_program())\n", 117 | "\n", 118 | "x_input = np.ones((1, 5)).astype('float32')\n", 119 | "exe.run(fetch_list=[y], feed={'x': x_input}) # [20, 26.]" 120 | ] 121 | }, 122 | { 123 | "cell_type": "code", 124 | "execution_count": 6, 125 | "metadata": { 126 | "ExecuteTime": { 127 | "end_time": "2018-07-25T07:12:03.860882Z", 128 | "start_time": "2018-07-25T07:12:03.848474Z" 129 | }, 130 | "scrolled": false 131 | }, 132 | "outputs": [ 133 | { 134 | "data": { 135 | "text/plain": [ 136 | "blocks {\n", 137 | " idx: 0\n", 138 | " parent_idx: -1\n", 139 | " vars {\n", 140 | " name: \"fc_0.b_0\"\n", 141 | " type {\n", 142 | " type: LOD_TENSOR\n", 143 | " lod_tensor {\n", 144 | " tensor {\n", 145 | " data_type: FP32\n", 146 | " dims: 2\n", 147 | " }\n", 148 | " }\n", 149 | " }\n", 150 | " persistable: true\n", 151 | " }\n", 152 | " vars {\n", 153 | " name: \"fc_0.w_0\"\n", 154 | " type {\n", 155 | " type: LOD_TENSOR\n", 156 | " lod_tensor {\n", 157 | " tensor {\n", 158 | " data_type: FP32\n", 159 | " dims: 5\n", 160 | " dims: 2\n", 161 | " }\n", 162 | " }\n", 163 | " }\n", 164 | " persistable: true\n", 165 | " }\n", 166 | " ops {\n", 167 | " outputs {\n", 168 | " parameter: \"Out\"\n", 169 | " arguments: \"fc_0.w_0\"\n", 170 | " }\n", 171 | " type: \"assign_value\"\n", 172 | " attrs {\n", 173 | " name: \"op_role_var\"\n", 174 | " type: STRINGS\n", 175 | " }\n", 176 | " attrs {\n", 177 | " name: \"int32_values\"\n", 178 | " type: INTS\n", 179 | " }\n", 180 | " attrs {\n", 181 | " name: \"op_role\"\n", 182 | " type: INT\n", 183 | " i: 0\n", 184 | " }\n", 185 | " attrs {\n", 186 | " name: \"fp32_values\"\n", 187 | " type: FLOATS\n", 188 | " floats: 0.0\n", 189 | " floats: 1.0\n", 190 | " floats: 2.0\n", 191 | " floats: 3.0\n", 192 | " floats: 4.0\n", 193 | " floats: 5.0\n", 194 | " floats: 6.0\n", 195 | " floats: 7.0\n", 196 | " floats: 8.0\n", 197 | " floats: 9.0\n", 198 | " }\n", 199 | " attrs {\n", 200 | " name: \"dtype\"\n", 201 | " type: INT\n", 202 | " i: 5\n", 203 | " }\n", 204 | " attrs {\n", 205 | " name: \"shape\"\n", 206 | " type: INTS\n", 207 | " ints: 5\n", 208 | " ints: 2\n", 209 | " }\n", 210 | " }\n", 211 | " ops {\n", 212 | " outputs {\n", 213 | " parameter: \"Out\"\n", 214 | " arguments: \"fc_0.b_0\"\n", 215 | " }\n", 216 | " type: \"assign_value\"\n", 217 | " attrs {\n", 218 | " name: \"op_role_var\"\n", 219 | " type: STRINGS\n", 220 | " }\n", 221 | " attrs {\n", 222 | " name: \"int32_values\"\n", 223 | " type: INTS\n", 224 | " }\n", 225 | " attrs {\n", 226 | " name: \"op_role\"\n", 227 | " type: INT\n", 228 | " i: 0\n", 229 | " }\n", 230 | " attrs {\n", 231 | " name: \"fp32_values\"\n", 232 | " type: FLOATS\n", 233 | " floats: 0.0\n", 234 | " floats: 1.0\n", 235 | " }\n", 236 | " attrs {\n", 237 | " name: \"dtype\"\n", 238 | " type: INT\n", 239 | " i: 5\n", 240 | " }\n", 241 | " attrs {\n", 242 | " name: \"shape\"\n", 243 | " type: INTS\n", 244 | " ints: 2\n", 245 | " }\n", 246 | " }\n", 247 | "}" 248 | ] 249 | }, 250 | "execution_count": 6, 251 | "metadata": {}, 252 | "output_type": "execute_result" 253 | } 254 | ], 255 | "source": [ 256 | "fluid.default_startup_program()" 257 | ] 258 | }, 259 | { 260 | "cell_type": "code", 261 | "execution_count": null, 262 | "metadata": {}, 263 | "outputs": [], 264 | "source": [] 265 | } 266 | ], 267 | "metadata": { 268 | "kernelspec": { 269 | "display_name": "Python 2", 270 | "language": "python", 271 | "name": "python2" 272 | }, 273 | "language_info": { 274 | "codemirror_mode": { 275 | "name": "ipython", 276 | "version": 2 277 | }, 278 | "file_extension": ".py", 279 | "mimetype": "text/x-python", 280 | "name": "python", 281 | "nbconvert_exporter": "python", 282 | "pygments_lexer": "ipython2", 283 | "version": "2.7.15" 284 | } 285 | }, 286 | "nbformat": 4, 287 | "nbformat_minor": 2 288 | } 289 | -------------------------------------------------------------------------------- /paddle/shared_params.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": { 7 | "ExecuteTime": { 8 | "end_time": "2018-07-25T07:45:21.010360Z", 9 | "start_time": "2018-07-25T07:45:20.233401Z" 10 | } 11 | }, 12 | "outputs": [], 13 | "source": [ 14 | "import paddle.fluid as fluid\n", 15 | "import paddle\n", 16 | "import numpy as np\n", 17 | "from paddle.fluid.debugger import draw_block_graphviz" 18 | ] 19 | }, 20 | { 21 | "cell_type": "markdown", 22 | "metadata": {}, 23 | "source": [ 24 | "## Shared params" 25 | ] 26 | }, 27 | { 28 | "cell_type": "code", 29 | "execution_count": 2, 30 | "metadata": { 31 | "ExecuteTime": { 32 | "end_time": "2018-07-25T07:45:21.038688Z", 33 | "start_time": "2018-07-25T07:45:21.014476Z" 34 | } 35 | }, 36 | "outputs": [], 37 | "source": [ 38 | "dataset = [\n", 39 | " [np.random.random(size=(128, 3, 10, 10)).astype('float32'), np.ones((128,1)).astype('float32')],\n", 40 | " [np.random.random(size=(128, 3, 24, 24)).astype('float32'), np.ones((128,1)).astype('float32')],\n", 41 | " [np.random.random(size=(128, 3, 48, 48)).astype('float32'), np.ones((128,1)).astype('float32')],\n", 42 | "]" 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "execution_count": 3, 48 | "metadata": { 49 | "ExecuteTime": { 50 | "end_time": "2018-07-25T07:45:21.087931Z", 51 | "start_time": "2018-07-25T07:45:21.041423Z" 52 | } 53 | }, 54 | "outputs": [], 55 | "source": [ 56 | "# Name all param_attr and bias_attr to share params\n", 57 | "def shared_net(image):\n", 58 | " y = fluid.layers.conv2d(name=\"conv2d\", input=image, num_filters=3, filter_size=3, act='relu', param_attr='conv2d_w', bias_attr='conv2d_b')\n", 59 | " y = fluid.layers.pool2d(name=\"pool2d\", input=y, pool_size=y.shape[2:])\n", 60 | " y = fluid.layers.fc(name='predict', input=y, size=1, param_attr='fc_w', bias_attr='fc_b')\n", 61 | " return y" 62 | ] 63 | }, 64 | { 65 | "cell_type": "code", 66 | "execution_count": 4, 67 | "metadata": { 68 | "ExecuteTime": { 69 | "end_time": "2018-07-25T07:45:21.155043Z", 70 | "start_time": "2018-07-25T07:45:21.091939Z" 71 | } 72 | }, 73 | "outputs": [], 74 | "source": [ 75 | "def train_net(name, shape):\n", 76 | " # Different program for diffent input size\n", 77 | " program = fluid.Program()\n", 78 | " with fluid.program_guard(program):\n", 79 | " label = fluid.layers.data(name='label' + name, shape=[1], dtype='float32')\n", 80 | " image = fluid.layers.data(name='image' + name, shape=shape, dtype='float32')\n", 81 | " predict = shared_net(image)\n", 82 | " infer_program = program.clone(for_test=True)\n", 83 | " cost = fluid.layers.square_error_cost(input=predict, label=label)\n", 84 | " avg_cost = fluid.layers.mean(cost)\n", 85 | " return image, label, predict, avg_cost, program, infer_program" 86 | ] 87 | }, 88 | { 89 | "cell_type": "code", 90 | "execution_count": 5, 91 | "metadata": { 92 | "ExecuteTime": { 93 | "end_time": "2018-07-25T07:45:21.285207Z", 94 | "start_time": "2018-07-25T07:45:21.159079Z" 95 | } 96 | }, 97 | "outputs": [], 98 | "source": [ 99 | "# Different program and optimizer for diffent input size\n", 100 | "image1, label1, predict1, avg_cost1, train_program1, infer_program1 = train_net(\"1\", [3,10,10])\n", 101 | "image2, label2, predict2, avg_cost2, train_program2, infer_program2 = train_net(\"2\", [3,24,24])\n", 102 | "image3, label3, predict3, avg_cost3, train_program3, infer_program3 = train_net(\"3\", [3,48,48])\n", 103 | "\n", 104 | "# And with different optimizer\n", 105 | "optimizer = fluid.optimizer.Adam(learning_rate=0.001)\n", 106 | "_ = optimizer.minimize(avg_cost1)\n", 107 | "optimizer2 = fluid.optimizer.Adam(learning_rate=0.001)\n", 108 | "_ = optimizer2.minimize(avg_cost2)\n", 109 | "optimizer3 = fluid.optimizer.Adam(learning_rate=0.001)\n", 110 | "_ = optimizer3.minimize(avg_cost3)" 111 | ] 112 | }, 113 | { 114 | "cell_type": "code", 115 | "execution_count": 6, 116 | "metadata": { 117 | "ExecuteTime": { 118 | "end_time": "2018-07-25T07:45:21.397059Z", 119 | "start_time": "2018-07-25T07:45:21.288134Z" 120 | } 121 | }, 122 | "outputs": [ 123 | { 124 | "name": "stderr", 125 | "output_type": "stream", 126 | "text": [ 127 | "WARNING:root:write block debug graph to g1.pdf\n", 128 | "WARNING:root:write block debug graph to g2.pdf\n" 129 | ] 130 | } 131 | ], 132 | "source": [ 133 | "draw_block_graphviz(avg_cost1.block, path=\"g1.pdf\")\n", 134 | "draw_block_graphviz(avg_cost2.block, path=\"g2.pdf\")" 135 | ] 136 | }, 137 | { 138 | "cell_type": "code", 139 | "execution_count": 7, 140 | "metadata": { 141 | "ExecuteTime": { 142 | "end_time": "2018-07-25T07:45:21.507372Z", 143 | "start_time": "2018-07-25T07:45:21.407372Z" 144 | } 145 | }, 146 | "outputs": [ 147 | { 148 | "data": { 149 | "text/plain": [ 150 | "[]" 151 | ] 152 | }, 153 | "execution_count": 7, 154 | "metadata": {}, 155 | "output_type": "execute_result" 156 | } 157 | ], 158 | "source": [ 159 | "exe = fluid.Executor(fluid.CPUPlace())\n", 160 | "exe.run(fluid.default_startup_program())" 161 | ] 162 | }, 163 | { 164 | "cell_type": "code", 165 | "execution_count": 8, 166 | "metadata": { 167 | "ExecuteTime": { 168 | "end_time": "2018-07-25T07:46:04.457408Z", 169 | "start_time": "2018-07-25T07:45:21.511338Z" 170 | } 171 | }, 172 | "outputs": [ 173 | { 174 | "name": "stdout", 175 | "output_type": "stream", 176 | "text": [ 177 | "(0, 4.21338, 6.256412, 7.2955246)\n", 178 | "(100, 0.12623334, 0.032384813, 0.11959931)\n", 179 | "(200, 0.1316835, 0.027014297, 0.103084795)\n", 180 | "(300, 0.12968615, 0.024039526, 0.093308955)\n", 181 | "(400, 0.12740204, 0.02158181, 0.08514366)\n", 182 | "(500, 0.124925956, 0.019551685, 0.07851617)\n", 183 | "(600, 0.12276098, 0.017818484, 0.072931156)\n", 184 | "(700, 0.12098904, 0.016324164, 0.06812219)\n", 185 | "(800, 0.11956181, 0.015047586, 0.06395402)\n", 186 | "(900, 0.118354075, 0.013962875, 0.060271367)\n" 187 | ] 188 | } 189 | ], 190 | "source": [ 191 | "for i in range(1000):\n", 192 | " c1 = exe.run(program=train_program1, fetch_list=[avg_cost1], feed={\n", 193 | " image1.name: dataset[0][0], label1.name: dataset[0][1]\n", 194 | " })\n", 195 | " c2 = exe.run(program=train_program2, fetch_list=[avg_cost2], feed={\n", 196 | " image2.name: dataset[1][0], label2.name: dataset[1][1]\n", 197 | " })\n", 198 | " c3 = exe.run(program=train_program3, fetch_list=[avg_cost3], feed={\n", 199 | " image3.name: dataset[2][0], label3.name: dataset[2][1]\n", 200 | " })\n", 201 | " if i % 100 == 0:\n", 202 | " print(i, c1[0][0], c2[0][0], c3[0][0])" 203 | ] 204 | }, 205 | { 206 | "cell_type": "code", 207 | "execution_count": 9, 208 | "metadata": { 209 | "ExecuteTime": { 210 | "end_time": "2018-07-25T07:46:04.587011Z", 211 | "start_time": "2018-07-25T07:46:04.462669Z" 212 | } 213 | }, 214 | "outputs": [ 215 | { 216 | "name": "stdout", 217 | "output_type": "stream", 218 | "text": [ 219 | "conv2d.b_1 conv2d.w_1\tpredict.b_1 predict.w_1\r\n" 220 | ] 221 | } 222 | ], 223 | "source": [ 224 | "fluid.io.save_params(exe, dirname=\"shared\")\n", 225 | "!ls shared" 226 | ] 227 | }, 228 | { 229 | "cell_type": "code", 230 | "execution_count": 10, 231 | "metadata": { 232 | "ExecuteTime": { 233 | "end_time": "2018-07-25T07:46:04.631188Z", 234 | "start_time": "2018-07-25T07:46:04.593238Z" 235 | } 236 | }, 237 | "outputs": [ 238 | { 239 | "name": "stdout", 240 | "output_type": "stream", 241 | "text": [ 242 | "[0.17281255]\n" 243 | ] 244 | } 245 | ], 246 | "source": [ 247 | "# For another inputsize, we can reuse the shared network\n", 248 | "img_size = 129\n", 249 | "image, label, predict, avg_cost, train_program1, infer_program1 = train_net(\"new_129\", [3,img_size,img_size])\n", 250 | "result, avg_cost = exe.run(program=train_program1, fetch_list=[predict, avg_cost], feed={\n", 251 | " image.name: np.random.random((8, 3, img_size, img_size)).astype('float32'),\n", 252 | " label.name: np.ones((8, 1)).astype('float32')\n", 253 | "})\n", 254 | "print(avg_cost)" 255 | ] 256 | }, 257 | { 258 | "cell_type": "code", 259 | "execution_count": 11, 260 | "metadata": { 261 | "ExecuteTime": { 262 | "end_time": "2018-07-25T07:46:04.670445Z", 263 | "start_time": "2018-07-25T07:46:04.634828Z" 264 | } 265 | }, 266 | "outputs": [ 267 | { 268 | "data": { 269 | "text/plain": [ 270 | "array([[1.3968543],\n", 271 | " [1.3360865],\n", 272 | " [1.4340291],\n", 273 | " [1.3299111],\n", 274 | " [1.3533896],\n", 275 | " [1.4627941],\n", 276 | " [1.3668158],\n", 277 | " [1.5841353]], dtype=float32)" 278 | ] 279 | }, 280 | "execution_count": 11, 281 | "metadata": {}, 282 | "output_type": "execute_result" 283 | } 284 | ], 285 | "source": [ 286 | "result" 287 | ] 288 | }, 289 | { 290 | "cell_type": "code", 291 | "execution_count": null, 292 | "metadata": {}, 293 | "outputs": [], 294 | "source": [] 295 | } 296 | ], 297 | "metadata": { 298 | "kernelspec": { 299 | "display_name": "Python 2", 300 | "language": "python", 301 | "name": "python2" 302 | }, 303 | "language_info": { 304 | "codemirror_mode": { 305 | "name": "ipython", 306 | "version": 2 307 | }, 308 | "file_extension": ".py", 309 | "mimetype": "text/x-python", 310 | "name": "python", 311 | "nbconvert_exporter": "python", 312 | "pygments_lexer": "ipython2", 313 | "version": "2.7.15" 314 | } 315 | }, 316 | "nbformat": 4, 317 | "nbformat_minor": 2 318 | } 319 | -------------------------------------------------------------------------------- /paddle/trainer_executor.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": { 7 | "ExecuteTime": { 8 | "end_time": "2018-07-25T07:43:59.226955Z", 9 | "start_time": "2018-07-25T07:43:58.461194Z" 10 | }, 11 | "id": "E3C4B2E7E6994ACD86D0F87E78316940", 12 | "scrolled": false 13 | }, 14 | "outputs": [], 15 | "source": [ 16 | "import paddle.fluid as fluid\n", 17 | "import paddle\n", 18 | "import numpy as np\n", 19 | "from paddle.fluid.debugger import draw_block_graphviz" 20 | ] 21 | }, 22 | { 23 | "cell_type": "code", 24 | "execution_count": 2, 25 | "metadata": { 26 | "ExecuteTime": { 27 | "end_time": "2018-07-25T07:43:59.238386Z", 28 | "start_time": "2018-07-25T07:43:59.231113Z" 29 | } 30 | }, 31 | "outputs": [], 32 | "source": [ 33 | "np.random.seed(0)\n", 34 | "X_train = np.random.random(size=(4, 3, 10, 10)).astype('float32')\n", 35 | "Y_train = np.array([[1], [1], [1], [1]]).astype('float32')" 36 | ] 37 | }, 38 | { 39 | "cell_type": "code", 40 | "execution_count": 3, 41 | "metadata": { 42 | "ExecuteTime": { 43 | "end_time": "2018-07-25T07:43:59.304214Z", 44 | "start_time": "2018-07-25T07:43:59.242200Z" 45 | } 46 | }, 47 | "outputs": [], 48 | "source": [ 49 | "def net(image):\n", 50 | " y = fluid.layers.conv2d(name=\"conv2d\", input=image, num_filters=3, filter_size=3, act='relu')\n", 51 | " y = fluid.layers.Print(y, summarize=10, message=\"Conv2d\")\n", 52 | " pool2d = fluid.layers.pool2d(name=\"pool2d\", input=y, pool_size=y.shape[2:])\n", 53 | " pool2d = fluid.layers.Print(pool2d, summarize=10, message=\"Pool2d\")\n", 54 | " y = fluid.layers.fc(name='predict', input=pool2d, size=1)\n", 55 | " return (y, pool2d)" 56 | ] 57 | }, 58 | { 59 | "cell_type": "markdown", 60 | "metadata": {}, 61 | "source": [ 62 | "## Trainer & Inferencer" 63 | ] 64 | }, 65 | { 66 | "cell_type": "code", 67 | "execution_count": 4, 68 | "metadata": { 69 | "ExecuteTime": { 70 | "end_time": "2018-07-25T07:43:59.369881Z", 71 | "start_time": "2018-07-25T07:43:59.309176Z" 72 | } 73 | }, 74 | "outputs": [], 75 | "source": [ 76 | "def reader():\n", 77 | " l = X_train.shape[0]\n", 78 | " for i in range(l):\n", 79 | " yield X_train[i], Y_train[i]\n", 80 | "\n", 81 | "train_reader = paddle.batch(reader, 2)" 82 | ] 83 | }, 84 | { 85 | "cell_type": "code", 86 | "execution_count": 5, 87 | "metadata": { 88 | "ExecuteTime": { 89 | "end_time": "2018-07-25T07:43:59.456896Z", 90 | "start_time": "2018-07-25T07:43:59.375332Z" 91 | } 92 | }, 93 | "outputs": [], 94 | "source": [ 95 | "def optimizer_program():\n", 96 | " return fluid.optimizer.Adam(learning_rate=0.001)" 97 | ] 98 | }, 99 | { 100 | "cell_type": "code", 101 | "execution_count": 6, 102 | "metadata": { 103 | "ExecuteTime": { 104 | "end_time": "2018-07-25T07:43:59.526589Z", 105 | "start_time": "2018-07-25T07:43:59.464582Z" 106 | } 107 | }, 108 | "outputs": [], 109 | "source": [ 110 | "def train_program():\n", 111 | " label = fluid.layers.data(name='label', shape=[1], dtype='float32')\n", 112 | " image = fluid.layers.data(name='image', shape=[3, 10, 10], dtype='float32')\n", 113 | " predict, _ = net(image)\n", 114 | " cost = fluid.layers.square_error_cost(input=predict, label=label)\n", 115 | " avg_cost = fluid.layers.mean(cost)\n", 116 | " return [avg_cost]" 117 | ] 118 | }, 119 | { 120 | "cell_type": "code", 121 | "execution_count": 7, 122 | "metadata": { 123 | "ExecuteTime": { 124 | "end_time": "2018-07-25T07:43:59.587376Z", 125 | "start_time": "2018-07-25T07:43:59.531316Z" 126 | } 127 | }, 128 | "outputs": [], 129 | "source": [ 130 | "def infer_program():\n", 131 | " image = fluid.layers.data(name='image', shape=[3, 10, 10], dtype='float32')\n", 132 | " predict, _= net(image)\n", 133 | " return predict" 134 | ] 135 | }, 136 | { 137 | "cell_type": "code", 138 | "execution_count": 8, 139 | "metadata": { 140 | "ExecuteTime": { 141 | "end_time": "2018-07-25T07:43:59.735124Z", 142 | "start_time": "2018-07-25T07:43:59.596569Z" 143 | } 144 | }, 145 | "outputs": [], 146 | "source": [ 147 | "trainer = fluid.Trainer(\n", 148 | " train_func=train_program, place=fluid.CPUPlace(), optimizer_func=optimizer_program)" 149 | ] 150 | }, 151 | { 152 | "cell_type": "code", 153 | "execution_count": 9, 154 | "metadata": { 155 | "ExecuteTime": { 156 | "end_time": "2018-07-25T07:43:59.804120Z", 157 | "start_time": "2018-07-25T07:43:59.738705Z" 158 | } 159 | }, 160 | "outputs": [], 161 | "source": [ 162 | "def event_handler(event):\n", 163 | " if isinstance(event, fluid.EndEpochEvent):\n", 164 | " trainer.save_params(\"./model\")" 165 | ] 166 | }, 167 | { 168 | "cell_type": "code", 169 | "execution_count": null, 170 | "metadata": { 171 | "ExecuteTime": { 172 | "start_time": "2018-07-25T07:43:58.474Z" 173 | } 174 | }, 175 | "outputs": [], 176 | "source": [ 177 | "trainer.train(\n", 178 | " num_epochs=200,\n", 179 | " event_handler=event_handler,\n", 180 | " reader=train_reader,\n", 181 | " feed_order=[\"image\", \"label\"]\n", 182 | ")" 183 | ] 184 | }, 185 | { 186 | "cell_type": "code", 187 | "execution_count": null, 188 | "metadata": { 189 | "ExecuteTime": { 190 | "start_time": "2018-07-25T07:43:58.475Z" 191 | } 192 | }, 193 | "outputs": [], 194 | "source": [ 195 | "!ls model" 196 | ] 197 | }, 198 | { 199 | "cell_type": "code", 200 | "execution_count": null, 201 | "metadata": { 202 | "ExecuteTime": { 203 | "start_time": "2018-07-25T07:43:58.476Z" 204 | } 205 | }, 206 | "outputs": [], 207 | "source": [ 208 | "infer = fluid.Inferencer(infer_func=infer_program, param_path=\"model\", place=fluid.CPUPlace())" 209 | ] 210 | }, 211 | { 212 | "cell_type": "code", 213 | "execution_count": null, 214 | "metadata": { 215 | "ExecuteTime": { 216 | "start_time": "2018-07-25T07:43:58.478Z" 217 | } 218 | }, 219 | "outputs": [], 220 | "source": [ 221 | "infer.infer({'image': X_train})" 222 | ] 223 | }, 224 | { 225 | "cell_type": "code", 226 | "execution_count": null, 227 | "metadata": { 228 | "ExecuteTime": { 229 | "start_time": "2018-07-25T07:43:58.479Z" 230 | } 231 | }, 232 | "outputs": [], 233 | "source": [ 234 | "with fluid.executor.scope_guard(infer.scope):\n", 235 | " result = infer.exe.run(infer.inference_program, fetch_list=[infer.predict_var], feed={'image': X_train})\n", 236 | "result" 237 | ] 238 | }, 239 | { 240 | "cell_type": "code", 241 | "execution_count": null, 242 | "metadata": { 243 | "ExecuteTime": { 244 | "start_time": "2018-07-25T07:43:58.481Z" 245 | } 246 | }, 247 | "outputs": [], 248 | "source": [ 249 | "# Draw a graph to find variable names\n", 250 | "draw_block_graphviz(infer.inference_program.blocks[0], path=\"g.pdf\")" 251 | ] 252 | }, 253 | { 254 | "cell_type": "code", 255 | "execution_count": null, 256 | "metadata": { 257 | "ExecuteTime": { 258 | "start_time": "2018-07-25T07:43:58.483Z" 259 | } 260 | }, 261 | "outputs": [], 262 | "source": [ 263 | "# fetch param and varialbe by name\n", 264 | "infer.exe.run(infer.inference_program, \n", 265 | " fetch_list=[infer.predict_var, 'conv2d.b_0', 'pool2d.tmp_0'], \n", 266 | " feed={'image': X_train},\n", 267 | " scope=infer.scope)" 268 | ] 269 | }, 270 | { 271 | "cell_type": "code", 272 | "execution_count": null, 273 | "metadata": { 274 | "ExecuteTime": { 275 | "start_time": "2018-07-25T07:43:58.484Z" 276 | } 277 | }, 278 | "outputs": [], 279 | "source": [ 280 | "b = fluid.global_scope().find_var('conv2d.b_0') # Trainer & Inferencer runs in a new scope\n", 281 | "b, type(b)" 282 | ] 283 | }, 284 | { 285 | "cell_type": "markdown", 286 | "metadata": {}, 287 | "source": [ 288 | "## Executor" 289 | ] 290 | }, 291 | { 292 | "cell_type": "code", 293 | "execution_count": null, 294 | "metadata": { 295 | "ExecuteTime": { 296 | "start_time": "2018-07-25T07:43:58.486Z" 297 | } 298 | }, 299 | "outputs": [], 300 | "source": [ 301 | "label = fluid.layers.data(name='label', shape=[1], dtype='float32')\n", 302 | "image = fluid.layers.data(name='image', shape=[3, 10, 10], dtype='float32')\n", 303 | "[predict, pool2d] = net(image)\n", 304 | "inference_program = fluid.default_main_program().clone(for_test=True)" 305 | ] 306 | }, 307 | { 308 | "cell_type": "code", 309 | "execution_count": null, 310 | "metadata": { 311 | "ExecuteTime": { 312 | "start_time": "2018-07-25T07:43:58.487Z" 313 | } 314 | }, 315 | "outputs": [], 316 | "source": [ 317 | "cost = fluid.layers.square_error_cost(input=predict, label=label)\n", 318 | "avg_cost = fluid.layers.mean(cost)" 319 | ] 320 | }, 321 | { 322 | "cell_type": "code", 323 | "execution_count": null, 324 | "metadata": { 325 | "ExecuteTime": { 326 | "start_time": "2018-07-25T07:43:58.489Z" 327 | } 328 | }, 329 | "outputs": [], 330 | "source": [ 331 | "optimizer = fluid.optimizer.Adam(learning_rate=0.01)" 332 | ] 333 | }, 334 | { 335 | "cell_type": "code", 336 | "execution_count": null, 337 | "metadata": { 338 | "ExecuteTime": { 339 | "start_time": "2018-07-25T07:43:58.491Z" 340 | } 341 | }, 342 | "outputs": [], 343 | "source": [ 344 | "_ = optimizer.minimize(avg_cost)" 345 | ] 346 | }, 347 | { 348 | "cell_type": "code", 349 | "execution_count": null, 350 | "metadata": { 351 | "ExecuteTime": { 352 | "start_time": "2018-07-25T07:43:58.492Z" 353 | } 354 | }, 355 | "outputs": [], 356 | "source": [ 357 | "exe = fluid.Executor(fluid.CPUPlace())" 358 | ] 359 | }, 360 | { 361 | "cell_type": "code", 362 | "execution_count": null, 363 | "metadata": { 364 | "ExecuteTime": { 365 | "start_time": "2018-07-25T07:43:58.494Z" 366 | } 367 | }, 368 | "outputs": [], 369 | "source": [ 370 | "exe.run(fluid.default_startup_program())" 371 | ] 372 | }, 373 | { 374 | "cell_type": "code", 375 | "execution_count": null, 376 | "metadata": { 377 | "ExecuteTime": { 378 | "start_time": "2018-07-25T07:43:58.496Z" 379 | } 380 | }, 381 | "outputs": [], 382 | "source": [ 383 | "feed={'image': X_train, 'label': Y_train}" 384 | ] 385 | }, 386 | { 387 | "cell_type": "code", 388 | "execution_count": null, 389 | "metadata": { 390 | "ExecuteTime": { 391 | "start_time": "2018-07-25T07:43:58.497Z" 392 | } 393 | }, 394 | "outputs": [], 395 | "source": [ 396 | "for i in range(200):\n", 397 | " cost, _ = exe.run(fetch_list=[avg_cost, pool2d], feed=feed)\n", 398 | " if i % 50 == 0:\n", 399 | " print(\"Epoch %i, cost %f\" % (i, cost))\n", 400 | "print(\"Epoch %i, cost %f\" % (i, cost))" 401 | ] 402 | }, 403 | { 404 | "cell_type": "code", 405 | "execution_count": null, 406 | "metadata": { 407 | "ExecuteTime": { 408 | "start_time": "2018-07-25T07:43:58.500Z" 409 | } 410 | }, 411 | "outputs": [], 412 | "source": [ 413 | "fluid.io.save_params(exe, dirname=\"exe-model\")\n", 414 | "!ls exe-model" 415 | ] 416 | }, 417 | { 418 | "cell_type": "code", 419 | "execution_count": null, 420 | "metadata": { 421 | "ExecuteTime": { 422 | "start_time": "2018-07-25T07:43:58.501Z" 423 | } 424 | }, 425 | "outputs": [], 426 | "source": [ 427 | "exe.run(inference_program, fetch_list=[predict], feed=feed)" 428 | ] 429 | }, 430 | { 431 | "cell_type": "code", 432 | "execution_count": null, 433 | "metadata": { 434 | "ExecuteTime": { 435 | "start_time": "2018-07-25T07:43:58.503Z" 436 | } 437 | }, 438 | "outputs": [], 439 | "source": [ 440 | "fluid.io.load_params(exe, dirname=\"exe-model\")" 441 | ] 442 | } 443 | ], 444 | "metadata": { 445 | "kernelspec": { 446 | "display_name": "Python 2", 447 | "language": "python", 448 | "name": "python2" 449 | }, 450 | "language_info": { 451 | "codemirror_mode": { 452 | "name": "ipython", 453 | "version": 2 454 | }, 455 | "file_extension": ".py", 456 | "mimetype": "text/x-python", 457 | "name": "python", 458 | "nbconvert_exporter": "python", 459 | "pygments_lexer": "ipython2", 460 | "version": "2.7.15" 461 | } 462 | }, 463 | "nbformat": 4, 464 | "nbformat_minor": 1 465 | } 466 | -------------------------------------------------------------------------------- /paddle/recordio.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "[How to use RecordIO in Fluid](https://github.com/PaddlePaddle/Paddle/blob/25241e9e5e8f691465a9dbdce2aa38344cbd05a0/doc/fluid/howto/cluster/fluid_recordio.md)" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 1, 13 | "metadata": { 14 | "ExecuteTime": { 15 | "end_time": "2018-08-03T02:43:13.571826Z", 16 | "start_time": "2018-08-03T02:43:12.798621Z" 17 | } 18 | }, 19 | "outputs": [], 20 | "source": [ 21 | "import paddle.fluid as fluid\n", 22 | "import paddle\n", 23 | "import numpy as np" 24 | ] 25 | }, 26 | { 27 | "cell_type": "markdown", 28 | "metadata": {}, 29 | "source": [ 30 | "## Fixed-Size" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": 2, 36 | "metadata": { 37 | "ExecuteTime": { 38 | "end_time": "2018-08-03T02:43:13.582768Z", 39 | "start_time": "2018-08-03T02:43:13.575799Z" 40 | } 41 | }, 42 | "outputs": [], 43 | "source": [ 44 | "X = np.random.random((64, 3, 24, 24)).astype('float32')\n", 45 | "Y = np.random.randint(0, 1, (128, 1)).astype('int64')\n", 46 | "BATCH_SIZE = 16" 47 | ] 48 | }, 49 | { 50 | "cell_type": "code", 51 | "execution_count": 3, 52 | "metadata": { 53 | "ExecuteTime": { 54 | "end_time": "2018-08-03T02:43:13.669214Z", 55 | "start_time": "2018-08-03T02:43:13.586951Z" 56 | } 57 | }, 58 | "outputs": [ 59 | { 60 | "data": { 61 | "text/plain": [ 62 | "13821.191" 63 | ] 64 | }, 65 | "execution_count": 3, 66 | "metadata": {}, 67 | "output_type": "execute_result" 68 | } 69 | ], 70 | "source": [ 71 | "## Before\n", 72 | "np.sum(X[:16])" 73 | ] 74 | }, 75 | { 76 | "cell_type": "code", 77 | "execution_count": 4, 78 | "metadata": { 79 | "ExecuteTime": { 80 | "end_time": "2018-08-03T02:43:13.725356Z", 81 | "start_time": "2018-08-03T02:43:13.677618Z" 82 | } 83 | }, 84 | "outputs": [], 85 | "source": [ 86 | "def reader_creator():\n", 87 | " for i in range(len(X)):\n", 88 | " yield X[i], Y[i]\n", 89 | "reader = reader_creator()" 90 | ] 91 | }, 92 | { 93 | "cell_type": "code", 94 | "execution_count": 5, 95 | "metadata": { 96 | "ExecuteTime": { 97 | "end_time": "2018-08-03T02:43:13.785072Z", 98 | "start_time": "2018-08-03T02:43:13.729186Z" 99 | } 100 | }, 101 | "outputs": [], 102 | "source": [ 103 | "batch_reader = paddle.batch(reader_creator, batch_size=1)" 104 | ] 105 | }, 106 | { 107 | "cell_type": "code", 108 | "execution_count": 6, 109 | "metadata": { 110 | "ExecuteTime": { 111 | "end_time": "2018-08-03T02:43:13.842913Z", 112 | "start_time": "2018-08-03T02:43:13.792795Z" 113 | } 114 | }, 115 | "outputs": [], 116 | "source": [ 117 | "main_program = fluid.Program()\n", 118 | "startup_program = fluid.Program()\n", 119 | "\n", 120 | "with fluid.program_guard(main_program, startup_program):\n", 121 | " img = fluid.layers.data(name=\"image\", shape=[3, 24, 24], dtype='float32')\n", 122 | " label = fluid.layers.data(name=\"label\", shape=[1], dtype=\"int64\")\n", 123 | " feeder = fluid.DataFeeder(feed_list=[img, label], place=fluid.CPUPlace())" 124 | ] 125 | }, 126 | { 127 | "cell_type": "code", 128 | "execution_count": 7, 129 | "metadata": { 130 | "ExecuteTime": { 131 | "end_time": "2018-08-03T02:43:13.990782Z", 132 | "start_time": "2018-08-03T02:43:13.847732Z" 133 | } 134 | }, 135 | "outputs": [ 136 | { 137 | "data": { 138 | "text/plain": [ 139 | "64" 140 | ] 141 | }, 142 | "execution_count": 7, 143 | "metadata": {}, 144 | "output_type": "execute_result" 145 | } 146 | ], 147 | "source": [ 148 | "fluid.recordio_writer.convert_reader_to_recordio_file(\n", 149 | " \"record.recordio\", feeder=feeder, reader_creator=batch_reader)" 150 | ] 151 | }, 152 | { 153 | "cell_type": "code", 154 | "execution_count": 8, 155 | "metadata": { 156 | "ExecuteTime": { 157 | "end_time": "2018-08-03T02:43:14.214888Z", 158 | "start_time": "2018-08-03T02:43:13.994562Z" 159 | } 160 | }, 161 | "outputs": [ 162 | { 163 | "name": "stdout", 164 | "output_type": "stream", 165 | "text": [ 166 | "440 record.recordio\r\n" 167 | ] 168 | } 169 | ], 170 | "source": [ 171 | "!ls record.recordio -s" 172 | ] 173 | }, 174 | { 175 | "cell_type": "code", 176 | "execution_count": 9, 177 | "metadata": { 178 | "ExecuteTime": { 179 | "end_time": "2018-08-03T02:43:14.243762Z", 180 | "start_time": "2018-08-03T02:43:14.222602Z" 181 | } 182 | }, 183 | "outputs": [], 184 | "source": [ 185 | "main_program = fluid.Program()\n", 186 | "startup_program = fluid.Program()\n", 187 | "\n", 188 | "with fluid.program_guard(main_program, startup_program):\n", 189 | " data_file = fluid.layers.open_recordio_file(\n", 190 | " filename=\"record.recordio\",\n", 191 | " shapes=[[-1, 3, 24, 24], [-1, 1]],\n", 192 | " lod_levels=[0, 0],\n", 193 | " dtypes=[\"float32\", \"int64\"],\n", 194 | " pass_num=100\n", 195 | " )\n", 196 | " data_file = fluid.layers.io.batch(data_file, batch_size=BATCH_SIZE)\n", 197 | " image, label = fluid.layers.read_file(data_file)" 198 | ] 199 | }, 200 | { 201 | "cell_type": "code", 202 | "execution_count": 10, 203 | "metadata": { 204 | "ExecuteTime": { 205 | "end_time": "2018-08-03T02:43:14.334385Z", 206 | "start_time": "2018-08-03T02:43:14.248779Z" 207 | } 208 | }, 209 | "outputs": [ 210 | { 211 | "data": { 212 | "text/plain": [ 213 | "[]" 214 | ] 215 | }, 216 | "execution_count": 10, 217 | "metadata": {}, 218 | "output_type": "execute_result" 219 | } 220 | ], 221 | "source": [ 222 | "exe = fluid.executor.Executor(fluid.CPUPlace())\n", 223 | "exe.run(startup_program)" 224 | ] 225 | }, 226 | { 227 | "cell_type": "code", 228 | "execution_count": 11, 229 | "metadata": { 230 | "ExecuteTime": { 231 | "end_time": "2018-08-03T02:43:14.396040Z", 232 | "start_time": "2018-08-03T02:43:14.341715Z" 233 | } 234 | }, 235 | "outputs": [], 236 | "source": [ 237 | "X, y = exe.run(main_program, fetch_list=[image, label])" 238 | ] 239 | }, 240 | { 241 | "cell_type": "code", 242 | "execution_count": 12, 243 | "metadata": { 244 | "ExecuteTime": { 245 | "end_time": "2018-08-03T02:43:14.463652Z", 246 | "start_time": "2018-08-03T02:43:14.403108Z" 247 | } 248 | }, 249 | "outputs": [ 250 | { 251 | "data": { 252 | "text/plain": [ 253 | "((16, 3, 24, 24), (16, 1))" 254 | ] 255 | }, 256 | "execution_count": 12, 257 | "metadata": {}, 258 | "output_type": "execute_result" 259 | } 260 | ], 261 | "source": [ 262 | "X.shape, y.shape" 263 | ] 264 | }, 265 | { 266 | "cell_type": "code", 267 | "execution_count": 13, 268 | "metadata": { 269 | "ExecuteTime": { 270 | "end_time": "2018-08-03T02:43:14.519830Z", 271 | "start_time": "2018-08-03T02:43:14.468320Z" 272 | } 273 | }, 274 | "outputs": [ 275 | { 276 | "data": { 277 | "text/plain": [ 278 | "13821.191" 279 | ] 280 | }, 281 | "execution_count": 13, 282 | "metadata": {}, 283 | "output_type": "execute_result" 284 | } 285 | ], 286 | "source": [ 287 | "# Same as before\n", 288 | "np.sum(X)" 289 | ] 290 | }, 291 | { 292 | "cell_type": "markdown", 293 | "metadata": { 294 | "ExecuteTime": { 295 | "end_time": "2018-08-03T02:26:27.393494Z", 296 | "start_time": "2018-08-03T02:26:27.386637Z" 297 | } 298 | }, 299 | "source": [ 300 | "## Dynamic Size" 301 | ] 302 | }, 303 | { 304 | "cell_type": "code", 305 | "execution_count": 14, 306 | "metadata": { 307 | "ExecuteTime": { 308 | "end_time": "2018-08-03T02:43:14.567037Z", 309 | "start_time": "2018-08-03T02:43:14.523642Z" 310 | } 311 | }, 312 | "outputs": [], 313 | "source": [ 314 | "def dynamic_creator():\n", 315 | " for i in range(1, 128):\n", 316 | " X = np.arange(3 * i * i).reshape(3, i, i).astype('float32')\n", 317 | " Y = np.random.randint(0, i)\n", 318 | " yield X, Y\n", 319 | "reader = dynamic_creator()" 320 | ] 321 | }, 322 | { 323 | "cell_type": "code", 324 | "execution_count": 15, 325 | "metadata": { 326 | "ExecuteTime": { 327 | "end_time": "2018-08-03T02:43:14.627010Z", 328 | "start_time": "2018-08-03T02:43:14.570757Z" 329 | } 330 | }, 331 | "outputs": [], 332 | "source": [ 333 | "batch_reader = paddle.batch(dynamic_creator, batch_size=1)" 334 | ] 335 | }, 336 | { 337 | "cell_type": "code", 338 | "execution_count": 16, 339 | "metadata": { 340 | "ExecuteTime": { 341 | "end_time": "2018-08-03T02:43:14.685364Z", 342 | "start_time": "2018-08-03T02:43:14.634211Z" 343 | } 344 | }, 345 | "outputs": [], 346 | "source": [ 347 | "main_program = fluid.Program()\n", 348 | "startup_program = fluid.Program()\n", 349 | "\n", 350 | "with fluid.program_guard(main_program, startup_program):\n", 351 | " img = fluid.layers.data(name=\"image\", shape=[3, None, None], dtype='float32')\n", 352 | " label = fluid.layers.data(name=\"label\", shape=[1], dtype=\"int64\")\n", 353 | " feeder = fluid.DataFeeder(feed_list=[img, label], place=fluid.CPUPlace())" 354 | ] 355 | }, 356 | { 357 | "cell_type": "code", 358 | "execution_count": 17, 359 | "metadata": { 360 | "ExecuteTime": { 361 | "end_time": "2018-08-03T02:43:14.792430Z", 362 | "start_time": "2018-08-03T02:43:14.690240Z" 363 | } 364 | }, 365 | "outputs": [ 366 | { 367 | "data": { 368 | "text/plain": [ 369 | "127" 370 | ] 371 | }, 372 | "execution_count": 17, 373 | "metadata": {}, 374 | "output_type": "execute_result" 375 | } 376 | ], 377 | "source": [ 378 | "fluid.recordio_writer.convert_reader_to_recordio_file(\n", 379 | " \"dynamic_record.recordio\", feeder=feeder, reader_creator=batch_reader)" 380 | ] 381 | }, 382 | { 383 | "cell_type": "code", 384 | "execution_count": 18, 385 | "metadata": { 386 | "ExecuteTime": { 387 | "end_time": "2018-08-03T02:43:14.921641Z", 388 | "start_time": "2018-08-03T02:43:14.796258Z" 389 | } 390 | }, 391 | "outputs": [ 392 | { 393 | "name": "stdout", 394 | "output_type": "stream", 395 | "text": [ 396 | "8020 dynamic_record.recordio\r\n" 397 | ] 398 | } 399 | ], 400 | "source": [ 401 | "!ls dynamic_record.recordio -s" 402 | ] 403 | }, 404 | { 405 | "cell_type": "code", 406 | "execution_count": 19, 407 | "metadata": { 408 | "ExecuteTime": { 409 | "end_time": "2018-08-03T02:43:14.952295Z", 410 | "start_time": "2018-08-03T02:43:14.930016Z" 411 | } 412 | }, 413 | "outputs": [], 414 | "source": [ 415 | "main_program = fluid.Program()\n", 416 | "startup_program = fluid.Program()\n", 417 | "\n", 418 | "with fluid.program_guard(main_program, startup_program):\n", 419 | " data_file = fluid.layers.open_recordio_file(\n", 420 | " filename=\"dynamic_record.recordio\",\n", 421 | " shapes=[[-1, 3, 1, 1], # 1, 1 Doesn't matter\n", 422 | " [-1, 1]],\n", 423 | " lod_levels=[0, 0],\n", 424 | " dtypes=[\"float32\", \"int64\"],\n", 425 | " pass_num=100\n", 426 | " )\n", 427 | " data_file = fluid.layers.io.batch(data_file, batch_size=1) ## Batch size be 1 \n", 428 | " image, label = fluid.layers.read_file(data_file)" 429 | ] 430 | }, 431 | { 432 | "cell_type": "code", 433 | "execution_count": 20, 434 | "metadata": { 435 | "ExecuteTime": { 436 | "end_time": "2018-08-03T02:43:15.011864Z", 437 | "start_time": "2018-08-03T02:43:14.963224Z" 438 | } 439 | }, 440 | "outputs": [ 441 | { 442 | "data": { 443 | "text/plain": [ 444 | "[]" 445 | ] 446 | }, 447 | "execution_count": 20, 448 | "metadata": {}, 449 | "output_type": "execute_result" 450 | } 451 | ], 452 | "source": [ 453 | "exe = fluid.executor.Executor(fluid.CPUPlace())\n", 454 | "exe.run(startup_program)" 455 | ] 456 | }, 457 | { 458 | "cell_type": "code", 459 | "execution_count": 21, 460 | "metadata": { 461 | "ExecuteTime": { 462 | "end_time": "2018-08-03T02:43:15.097857Z", 463 | "start_time": "2018-08-03T02:43:15.015793Z" 464 | } 465 | }, 466 | "outputs": [ 467 | { 468 | "data": { 469 | "text/plain": [ 470 | "[array([[[[0.]],\n", 471 | " \n", 472 | " [[1.]],\n", 473 | " \n", 474 | " [[2.]]]], dtype=float32), array([[0]])]" 475 | ] 476 | }, 477 | "execution_count": 21, 478 | "metadata": {}, 479 | "output_type": "execute_result" 480 | } 481 | ], 482 | "source": [ 483 | "exe.run(main_program, fetch_list=[image, label])" 484 | ] 485 | }, 486 | { 487 | "cell_type": "code", 488 | "execution_count": 22, 489 | "metadata": { 490 | "ExecuteTime": { 491 | "end_time": "2018-08-03T02:43:15.146473Z", 492 | "start_time": "2018-08-03T02:43:15.101642Z" 493 | } 494 | }, 495 | "outputs": [ 496 | { 497 | "data": { 498 | "text/plain": [ 499 | "[array([[[[ 0., 1.],\n", 500 | " [ 2., 3.]],\n", 501 | " \n", 502 | " [[ 4., 5.],\n", 503 | " [ 6., 7.]],\n", 504 | " \n", 505 | " [[ 8., 9.],\n", 506 | " [10., 11.]]]], dtype=float32), array([[1]])]" 507 | ] 508 | }, 509 | "execution_count": 22, 510 | "metadata": {}, 511 | "output_type": "execute_result" 512 | } 513 | ], 514 | "source": [ 515 | "exe.run(main_program, fetch_list=[image, label])" 516 | ] 517 | }, 518 | { 519 | "cell_type": "code", 520 | "execution_count": null, 521 | "metadata": {}, 522 | "outputs": [], 523 | "source": [] 524 | } 525 | ], 526 | "metadata": { 527 | "kernelspec": { 528 | "display_name": "Python 2", 529 | "language": "python", 530 | "name": "python2" 531 | }, 532 | "language_info": { 533 | "codemirror_mode": { 534 | "name": "ipython", 535 | "version": 2 536 | }, 537 | "file_extension": ".py", 538 | "mimetype": "text/x-python", 539 | "name": "python", 540 | "nbconvert_exporter": "python", 541 | "pygments_lexer": "ipython2", 542 | "version": "2.7.15" 543 | } 544 | }, 545 | "nbformat": 4, 546 | "nbformat_minor": 2 547 | } 548 | -------------------------------------------------------------------------------- /paddle/view_saved_params.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": { 7 | "ExecuteTime": { 8 | "end_time": "2018-08-01T07:30:58.891523Z", 9 | "start_time": "2018-08-01T07:30:58.123369Z" 10 | } 11 | }, 12 | "outputs": [], 13 | "source": [ 14 | "import paddle.fluid as fluid\n", 15 | "import paddle\n", 16 | "import numpy as np" 17 | ] 18 | }, 19 | { 20 | "cell_type": "code", 21 | "execution_count": 2, 22 | "metadata": { 23 | "ExecuteTime": { 24 | "end_time": "2018-08-01T07:30:59.015060Z", 25 | "start_time": "2018-08-01T07:30:58.895640Z" 26 | } 27 | }, 28 | "outputs": [ 29 | { 30 | "name": "stdout", 31 | "output_type": "stream", 32 | "text": [ 33 | "conv2d_0.w_0\r\n" 34 | ] 35 | } 36 | ], 37 | "source": [ 38 | "!ls models/epoch_259 | grep conv | head -n 1" 39 | ] 40 | }, 41 | { 42 | "cell_type": "code", 43 | "execution_count": 3, 44 | "metadata": { 45 | "ExecuteTime": { 46 | "end_time": "2018-08-01T07:30:59.030396Z", 47 | "start_time": "2018-08-01T07:30:59.022840Z" 48 | } 49 | }, 50 | "outputs": [], 51 | "source": [ 52 | "exe = fluid.Executor(fluid.CPUPlace())\n", 53 | "program = fluid.Program()\n", 54 | "block = program.current_block()" 55 | ] 56 | }, 57 | { 58 | "cell_type": "code", 59 | "execution_count": 4, 60 | "metadata": { 61 | "ExecuteTime": { 62 | "end_time": "2018-08-01T07:30:59.100766Z", 63 | "start_time": "2018-08-01T07:30:59.038047Z" 64 | } 65 | }, 66 | "outputs": [], 67 | "source": [ 68 | "v = block.create_var(name=\"conv2d_0.w_0\",shape=[1], dtype='float32')" 69 | ] 70 | }, 71 | { 72 | "cell_type": "code", 73 | "execution_count": 5, 74 | "metadata": { 75 | "ExecuteTime": { 76 | "end_time": "2018-08-01T07:30:59.228492Z", 77 | "start_time": "2018-08-01T07:30:59.114762Z" 78 | } 79 | }, 80 | "outputs": [], 81 | "source": [ 82 | "fluid.io.load_vars(exe, \"models/epoch_259\", vars=[v])" 83 | ] 84 | }, 85 | { 86 | "cell_type": "code", 87 | "execution_count": 6, 88 | "metadata": { 89 | "ExecuteTime": { 90 | "end_time": "2018-08-01T07:30:59.299733Z", 91 | "start_time": "2018-08-01T07:30:59.232361Z" 92 | } 93 | }, 94 | "outputs": [], 95 | "source": [ 96 | "v = fluid.global_scope().find_var(\"conv2d_0.w_0\")" 97 | ] 98 | }, 99 | { 100 | "cell_type": "code", 101 | "execution_count": 7, 102 | "metadata": { 103 | "ExecuteTime": { 104 | "end_time": "2018-08-01T07:30:59.408142Z", 105 | "start_time": "2018-08-01T07:30:59.305209Z" 106 | } 107 | }, 108 | "outputs": [ 109 | { 110 | "data": { 111 | "text/plain": [ 112 | "array([[[[ 1.20054225e-34, 1.26970276e-34, -1.46688563e-34],\n", 113 | " [ 1.44232055e-34, 1.35073783e-34, -7.49601635e-34],\n", 114 | " [ 1.14180748e-34, 1.11791212e-34, 1.16035240e-34]],\n", 115 | "\n", 116 | " [[ 1.21382075e-34, 1.47352705e-34, -1.48725703e-34],\n", 117 | " [ 1.63999542e-34, 1.34376855e-34, 1.47257219e-34],\n", 118 | " [-1.04197803e-33, 1.07197417e-34, 1.18048687e-34]],\n", 119 | "\n", 120 | " [[ 1.32231440e-34, 1.44930188e-34, 1.64868170e-33],\n", 121 | " [ 1.32633140e-34, 1.24502748e-34, 1.70030264e-34],\n", 122 | " [-3.82254600e-34, 1.34157070e-34, 1.41326001e-34]]],\n", 123 | "\n", 124 | "\n", 125 | " [[[-4.23182035e-03, 2.49961317e-02, -9.02260654e-03],\n", 126 | " [ 9.26054865e-02, -1.14236936e-01, -1.94295019e-01],\n", 127 | " [ 2.72465013e-02, 1.67676492e-03, -1.14957467e-01]],\n", 128 | "\n", 129 | " [[ 1.15535207e-01, -1.64367314e-02, -1.83496997e-01],\n", 130 | " [ 1.41454190e-01, -2.94967175e-01, -4.90740240e-01],\n", 131 | " [ 8.92807767e-02, -6.03696406e-02, -1.98393703e-01]],\n", 132 | "\n", 133 | " [[ 6.46485165e-02, 1.91468373e-02, -9.21798199e-02],\n", 134 | " [ 1.34104952e-01, -1.87569946e-01, -3.51821721e-01],\n", 135 | " [ 2.57654507e-02, -1.70747768e-02, -1.40358567e-01]]],\n", 136 | "\n", 137 | "\n", 138 | " [[[-1.27493534e-02, 2.73171924e-02, 2.32783239e-03],\n", 139 | " [ 2.39697937e-02, -1.25651583e-01, -1.67616516e-01],\n", 140 | " [-2.17663590e-02, 1.21159241e-01, 1.80762544e-01]],\n", 141 | "\n", 142 | " [[ 2.35420167e-02, 3.39316502e-02, -4.49013971e-02],\n", 143 | " [ 5.38015626e-02, -5.00741661e-01, -6.61131382e-01],\n", 144 | " [ 4.97711822e-04, 4.05063540e-01, 6.69052780e-01]],\n", 145 | "\n", 146 | " [[-1.01147834e-02, 3.93103510e-02, 8.88039917e-03],\n", 147 | " [ 4.22593988e-02, -2.66653031e-01, -3.67600322e-01],\n", 148 | " [ 3.09928483e-03, 2.19897479e-01, 3.74356389e-01]]],\n", 149 | "\n", 150 | "\n", 151 | " [[[ 5.13726361e-02, 1.58450112e-01, 2.03695595e-01],\n", 152 | " [ 1.20748930e-01, 2.52875298e-01, 2.92344004e-01],\n", 153 | " [ 1.40366927e-01, 2.47654215e-01, 2.55210817e-01]],\n", 154 | "\n", 155 | " [[-7.86251873e-02, -2.55955517e-01, -2.17782497e-01],\n", 156 | " [-2.26424232e-01, -4.23868477e-01, -3.50361168e-01],\n", 157 | " [-2.00344294e-01, -3.60168010e-01, -2.68307477e-01]],\n", 158 | "\n", 159 | " [[ 9.73600447e-02, 5.38732931e-02, 3.69612165e-02],\n", 160 | " [ 1.04397558e-01, 4.01085764e-02, 2.81675216e-02],\n", 161 | " [ 5.86902425e-02, 5.85627034e-02, 1.32821828e-01]]],\n", 162 | "\n", 163 | "\n", 164 | " [[[ 7.59645924e-02, -3.50867286e-02, -6.33968636e-02],\n", 165 | " [-7.63096521e-03, -1.48293942e-01, -1.34998187e-01],\n", 166 | " [-7.16610178e-02, -1.62183985e-01, -1.09531790e-01]],\n", 167 | "\n", 168 | " [[-9.32874754e-02, -2.08487660e-01, -1.71321645e-01],\n", 169 | " [-2.35489175e-01, -4.10575092e-01, -3.54047924e-01],\n", 170 | " [-2.05876932e-01, -3.66328388e-01, -3.41038048e-01]],\n", 171 | "\n", 172 | " [[-2.69049290e-03, 2.49393702e-01, 2.71071583e-01],\n", 173 | " [ 2.65964925e-01, 5.48801064e-01, 5.36212027e-01],\n", 174 | " [ 2.41974160e-01, 5.08314848e-01, 4.74240780e-01]]],\n", 175 | "\n", 176 | "\n", 177 | " [[[ 8.38393271e-02, -8.43135733e-03, -2.78634112e-02],\n", 178 | " [-3.94173078e-02, -1.87281713e-01, 4.25731158e-03],\n", 179 | " [-4.67875116e-02, 2.65989304e-01, -2.44873334e-02]],\n", 180 | "\n", 181 | " [[ 3.04862142e-01, -1.42197207e-01, -1.01933993e-01],\n", 182 | " [-2.11938888e-01, -6.18799746e-01, -4.47009839e-02],\n", 183 | " [-3.74950655e-02, 8.11967015e-01, 7.18111172e-02]],\n", 184 | "\n", 185 | " [[ 1.49569422e-01, -4.88907173e-02, -7.06802532e-02],\n", 186 | " [-1.20912969e-01, -3.30245048e-01, -3.06555144e-02],\n", 187 | " [-7.49055110e-03, 4.67851192e-01, 1.20583819e-02]]],\n", 188 | "\n", 189 | "\n", 190 | " [[[-2.64350921e-02, -1.31662488e-02, 1.10732898e-01],\n", 191 | " [ 2.25085150e-02, -6.04358464e-02, -1.06333889e-01],\n", 192 | " [ 4.56572697e-03, -4.38638963e-02, -2.12402880e-01]],\n", 193 | "\n", 194 | " [[ 3.32834050e-02, 2.14872248e-02, 1.05447777e-01],\n", 195 | " [ 2.64673382e-02, -1.94818020e-01, -3.46133858e-01],\n", 196 | " [-2.70774923e-02, -2.27683917e-01, -5.66627741e-01]],\n", 197 | "\n", 198 | " [[ 3.03758238e-03, 1.31220743e-02, 1.33508444e-01],\n", 199 | " [ 1.55303963e-02, -6.60255179e-02, -1.67354405e-01],\n", 200 | " [-5.42673562e-03, -7.53092095e-02, -3.40104491e-01]]],\n", 201 | "\n", 202 | "\n", 203 | " [[[ 2.45885439e-02, 1.35273442e-01, 9.66677517e-02],\n", 204 | " [ 2.87077352e-02, 1.07796408e-01, 1.82195250e-02],\n", 205 | " [-3.90363857e-02, 2.09600702e-02, -9.49614421e-02]],\n", 206 | "\n", 207 | " [[ 3.57687138e-02, 2.40896240e-01, 1.84105724e-01],\n", 208 | " [ 5.52026033e-02, 2.38118351e-01, 9.11881700e-02],\n", 209 | " [-4.89374846e-02, 4.74177161e-03, -1.95865557e-01]],\n", 210 | "\n", 211 | " [[-9.29429196e-03, 2.08690658e-01, 1.76438645e-01],\n", 212 | " [ 5.44534549e-02, 1.91839978e-01, 7.09725171e-02],\n", 213 | " [-4.27464321e-02, -2.05721310e-03, -1.74881682e-01]]],\n", 214 | "\n", 215 | "\n", 216 | " [[[ 1.66322693e-34, 7.54589726e-34, -1.62904656e-34],\n", 217 | " [-1.53630258e-34, 1.42239374e-34, -1.60765417e-34],\n", 218 | " [ 1.58729711e-34, -1.57890415e-34, 1.31734564e-34]],\n", 219 | "\n", 220 | " [[ 1.25630339e-34, -1.72231148e-34, -1.73431840e-34],\n", 221 | " [ 1.26727727e-34, 1.51298681e-34, 1.21611767e-34],\n", 222 | " [-1.70940331e-34, 1.37781013e-34, -1.72421431e-34]],\n", 223 | "\n", 224 | " [[ 1.19341593e-34, 1.26798613e-34, 1.42397629e-34],\n", 225 | " [ 1.11115429e-34, 1.71140314e-34, 1.26985739e-34],\n", 226 | " [ 1.26063848e-34, 1.18421149e-34, 1.18764201e-34]]],\n", 227 | "\n", 228 | "\n", 229 | " [[[-1.31866623e-34, 9.10565107e-34, -1.33707363e-34],\n", 230 | " [ 1.33753854e-34, -1.61449718e-34, -1.34896219e-34],\n", 231 | " [ 1.43746038e-34, 1.65365056e-34, 1.58307865e-34]],\n", 232 | "\n", 233 | " [[-1.29450236e-34, -2.15503453e-33, 1.70834525e-34],\n", 234 | " [-1.35622821e-34, 1.61563893e-34, -1.35861135e-34],\n", 235 | " [ 1.52404197e-34, 1.30841131e-34, 1.36578439e-34]],\n", 236 | "\n", 237 | " [[ 1.55324933e-34, 1.69421934e-34, -1.36854255e-34],\n", 238 | " [-1.40792620e-34, -1.65290164e-34, 1.67025453e-34],\n", 239 | " [ 1.41464868e-34, -1.64999229e-34, 1.74032030e-34]]],\n", 240 | "\n", 241 | "\n", 242 | " [[[ 1.44451633e-34, 1.57635939e-34, -1.43941315e-34],\n", 243 | " [-1.76986872e-34, 1.59318572e-34, 1.29003617e-34],\n", 244 | " [-1.29985385e-34, -1.50157178e-34, -1.38040884e-34]],\n", 245 | "\n", 246 | " [[ 1.73800479e-34, 1.35583137e-34, -1.34599602e-34],\n", 247 | " [ 1.46540042e-34, 1.35489270e-34, 1.29022627e-34],\n", 248 | " [ 1.52506330e-34, -1.34250317e-34, 1.74061670e-34]],\n", 249 | "\n", 250 | " [[-1.53352468e-34, -1.39205255e-34, 1.41159285e-34],\n", 251 | " [-1.76218703e-34, 1.35637446e-34, -3.13100566e-34],\n", 252 | " [-1.55961066e-34, 1.54605241e-34, 1.27232386e-34]]],\n", 253 | "\n", 254 | "\n", 255 | " [[[-3.61556560e-02, 1.25869334e-01, 6.55115768e-02],\n", 256 | " [ 3.23943347e-02, 3.41177285e-01, 3.75977725e-01],\n", 257 | " [-2.90204082e-02, 2.52556860e-01, 3.74863774e-01]],\n", 258 | "\n", 259 | " [[ 5.72798327e-02, 1.27808928e-01, 1.16232470e-01],\n", 260 | " [ 5.33857644e-02, 1.34726718e-01, 1.27929047e-01],\n", 261 | " [-1.05265807e-02, 6.80564791e-02, 6.18272014e-02]],\n", 262 | "\n", 263 | " [[-3.87501977e-02, -2.68670976e-01, -1.80106401e-01],\n", 264 | " [-9.24323052e-02, -4.76270467e-01, -5.00617623e-01],\n", 265 | " [ 4.96184677e-02, -3.19074869e-01, -4.43929315e-01]]],\n", 266 | "\n", 267 | "\n", 268 | " [[[ 3.89046520e-02, 1.59019530e-02, -1.17941555e-02],\n", 269 | " [ 9.89135653e-02, 6.23715594e-02, -7.59827271e-02],\n", 270 | " [ 6.05464773e-03, 1.51206762e-01, 3.14176455e-03]],\n", 271 | "\n", 272 | " [[ 8.25406387e-02, 4.66076806e-02, -1.81939811e-01],\n", 273 | " [ 1.77047208e-01, 2.26461679e-01, -7.13629723e-02],\n", 274 | " [ 1.10886181e-02, 3.70177418e-01, 1.63384005e-01]],\n", 275 | "\n", 276 | " [[ 6.94745034e-02, 2.61260364e-02, -8.79104063e-02],\n", 277 | " [ 1.25730947e-01, 1.17438473e-01, -8.95477459e-02],\n", 278 | " [-1.79622620e-02, 2.40220517e-01, 7.93443844e-02]]],\n", 279 | "\n", 280 | "\n", 281 | " [[[ 1.65941679e-34, 2.16816700e-33, -1.39891599e-34],\n", 282 | " [ 1.30523495e-34, 2.52613139e-34, 1.32856530e-34],\n", 283 | " [-2.63064868e-34, 1.31591450e-34, 1.61793791e-34]],\n", 284 | "\n", 285 | " [[-1.54989068e-34, -1.53178933e-34, -1.69080444e-34],\n", 286 | " [ 1.43325650e-34, 1.52876565e-34, -1.46088408e-33],\n", 287 | " [-1.37465283e-34, -1.37412029e-34, 1.36386594e-34]],\n", 288 | "\n", 289 | " [[-1.37642628e-34, 1.54707213e-34, 1.50670400e-34],\n", 290 | " [-1.51287374e-34, 9.61610582e-34, -1.36706733e-34],\n", 291 | " [-1.76888091e-34, -1.40172880e-34, 1.29853693e-34]]],\n", 292 | "\n", 293 | "\n", 294 | " [[[-1.71442097e-34, -1.44394661e-34, 1.50594797e-34],\n", 295 | " [-1.58761245e-34, 1.44516825e-34, -1.40276138e-34],\n", 296 | " [ 1.76535822e-34, -1.47312080e-34, -1.32824468e-34]],\n", 297 | "\n", 298 | " [[ 4.42167021e-34, -1.51127489e-34, -1.42122903e-34],\n", 299 | " [-1.38081338e-34, 2.34859712e-33, -1.25499714e-34],\n", 300 | " [ 1.50679389e-34, 1.54704883e-34, 1.63434237e-34]],\n", 301 | "\n", 302 | " [[-1.31131928e-34, -1.62606317e-34, -1.48925296e-34],\n", 303 | " [-1.30784468e-34, -1.43236576e-33, 1.60972839e-34],\n", 304 | " [-1.29818129e-34, -1.30249354e-34, -1.47928250e-34]]],\n", 305 | "\n", 306 | "\n", 307 | " [[[ 2.48024873e-02, 1.40902773e-01, 2.79257931e-02],\n", 308 | " [-1.19264565e-01, -1.47651866e-01, -3.39739695e-02],\n", 309 | " [ 4.52026725e-02, 4.96275313e-02, -1.30894482e-02]],\n", 310 | "\n", 311 | " [[ 2.83257544e-01, 4.91760194e-01, 1.87396899e-01],\n", 312 | " [-4.75624472e-01, -5.56242347e-01, -1.26463369e-01],\n", 313 | " [ 1.47407144e-01, 1.08184725e-01, -2.26781543e-04]],\n", 314 | "\n", 315 | " [[ 1.29240662e-01, 2.83741921e-01, 7.86473230e-02],\n", 316 | " [-2.29173377e-01, -2.95283824e-01, -7.23578185e-02],\n", 317 | " [ 7.74062872e-02, 6.85337931e-02, -2.01249886e-02]]],\n", 318 | "\n", 319 | "\n", 320 | " [[[-1.45243090e-02, 5.43150343e-02, -4.36400389e-03],\n", 321 | " [-2.01364592e-01, 1.54017270e-01, -4.95948009e-02],\n", 322 | " [-2.37642340e-02, 1.10104732e-01, -1.83464736e-02]],\n", 323 | "\n", 324 | " [[-1.25200108e-01, 2.44568124e-01, -4.90000919e-02],\n", 325 | " [-6.89327061e-01, 5.79025686e-01, -5.81069514e-02],\n", 326 | " [-1.67067170e-01, 3.82248282e-01, -1.77829802e-01]],\n", 327 | "\n", 328 | " [[-3.91555540e-02, 1.25783458e-01, -3.68970260e-02],\n", 329 | " [-3.66636485e-01, 2.65004694e-01, -5.13072796e-02],\n", 330 | " [-6.96651116e-02, 2.41851911e-01, -9.21359658e-02]]],\n", 331 | "\n", 332 | "\n", 333 | " [[[ 3.80142927e-02, 3.10068280e-02, -2.62456387e-02],\n", 334 | " [ 1.07190395e-02, -1.42873600e-01, 4.84233052e-02],\n", 335 | " [ 1.31246731e-01, -7.89678320e-02, -4.91007324e-03]],\n", 336 | "\n", 337 | " [[ 1.11154936e-01, 8.23828802e-02, -1.37308042e-03],\n", 338 | " [ 1.06490754e-01, -5.00171244e-01, 1.33682117e-01],\n", 339 | " [ 5.50666988e-01, -4.26509768e-01, -1.01409726e-01]],\n", 340 | "\n", 341 | " [[ 5.80342114e-02, 6.43518865e-02, -1.30100138e-02],\n", 342 | " [ 6.36520237e-02, -2.69764960e-01, 9.73463133e-02],\n", 343 | " [ 3.20764840e-01, -2.18334675e-01, -7.09225237e-02]]],\n", 344 | "\n", 345 | "\n", 346 | " [[[-6.79525137e-02, -2.65477121e-01, -2.46761039e-01],\n", 347 | " [-2.51371413e-01, -4.52682346e-01, -4.05918896e-01],\n", 348 | " [-1.91640630e-01, -3.90084118e-01, -2.89668947e-01]],\n", 349 | "\n", 350 | " [[ 2.59817958e-01, 4.20099854e-01, 4.71690685e-01],\n", 351 | " [ 4.21391398e-01, 5.93170106e-01, 6.85456812e-01],\n", 352 | " [ 3.86705726e-01, 5.56689560e-01, 5.84274173e-01]],\n", 353 | "\n", 354 | " [[-1.59206867e-01, -1.81409284e-01, -2.05330506e-01],\n", 355 | " [-1.54793069e-01, -2.08586156e-01, -2.54213244e-01],\n", 356 | " [-1.42763659e-01, -2.30400294e-01, -2.37586826e-01]]],\n", 357 | "\n", 358 | "\n", 359 | " [[[ 3.38108428e-02, 1.51013001e-03, 1.32828234e-02],\n", 360 | " [ 4.22449559e-02, 7.69665763e-02, 1.35559022e-01],\n", 361 | " [-1.29357461e-04, -2.94129997e-02, 7.63152838e-02]],\n", 362 | "\n", 363 | " [[ 1.17378682e-01, 4.20437530e-02, 5.85704446e-02],\n", 364 | " [ 1.15711249e-01, 9.94178653e-02, 1.72956496e-01],\n", 365 | " [ 9.71605331e-02, -4.93625179e-04, 1.35265172e-01]],\n", 366 | "\n", 367 | " [[ 6.95354491e-02, 1.26590841e-02, 4.03905846e-02],\n", 368 | " [ 1.77398883e-02, 1.52602959e-02, 1.05419457e-01],\n", 369 | " [-6.86266227e-03, -7.40231872e-02, 7.69837052e-02]]],\n", 370 | "\n", 371 | "\n", 372 | " [[[-8.05592909e-02, -5.46005508e-03, -1.16062732e-02],\n", 373 | " [ 8.30554366e-02, 5.67879602e-02, 2.13994924e-03],\n", 374 | " [-1.37238622e-01, -1.09149709e-01, 1.09457865e-01]],\n", 375 | "\n", 376 | " [[ 3.20492173e-03, 4.21823189e-02, 3.93729620e-02],\n", 377 | " [ 8.04878250e-02, -2.03324743e-02, -1.24755064e-02],\n", 378 | " [-3.19559485e-01, -3.32226127e-01, 5.06038591e-02]],\n", 379 | "\n", 380 | " [[-5.62642608e-03, 3.74060906e-02, 1.30546279e-02],\n", 381 | " [ 1.39758527e-01, 1.31322267e-02, 2.02263575e-02],\n", 382 | " [-2.52297580e-01, -2.70071059e-01, 1.16843402e-01]]],\n", 383 | "\n", 384 | "\n", 385 | " [[[-5.33731766e-02, 5.74094951e-02, 3.23482938e-02],\n", 386 | " [ 9.90308896e-02, 1.45538181e-01, 2.91542336e-02],\n", 387 | " [ 8.57264027e-02, 1.02381960e-01, 1.08865071e-02]],\n", 388 | "\n", 389 | " [[ 2.60747790e-01, 3.51777345e-01, 3.62846822e-01],\n", 390 | " [ 2.95608103e-01, 3.48795354e-01, 3.31979990e-01],\n", 391 | " [ 1.78470224e-01, 2.33983859e-01, 2.49734342e-01]],\n", 392 | "\n", 393 | " [[-1.95188746e-01, -4.14566904e-01, -3.33891302e-01],\n", 394 | " [-3.42621773e-01, -4.87147421e-01, -3.72169316e-01],\n", 395 | " [-3.28316092e-01, -3.87404531e-01, -2.57971495e-01]]],\n", 396 | "\n", 397 | "\n", 398 | " [[[-1.49448483e-34, 1.68339986e-34, 1.41464225e-34],\n", 399 | " [ 1.33547201e-34, -1.40287077e-34, -1.58556960e-33],\n", 400 | " [-1.46899738e-34, 1.35619561e-34, 1.32676555e-34]],\n", 401 | "\n", 402 | " [[ 1.36367848e-34, 1.35178292e-34, 1.77101735e-34],\n", 403 | " [-1.23265864e-33, 3.69891338e-34, -1.58203551e-34],\n", 404 | " [-1.60864978e-34, -1.59929714e-34, 1.84272653e-34]],\n", 405 | "\n", 406 | " [[ 1.50255981e-34, 1.64462875e-34, -1.33070173e-34],\n", 407 | " [ 1.55214880e-34, 1.33418448e-34, 1.56795265e-34],\n", 408 | " [ 1.43532877e-34, 1.55106881e-34, -1.43346451e-34]]],\n", 409 | "\n", 410 | "\n", 411 | " [[[ 1.73191447e-02, -9.26317349e-02, -1.36979789e-01],\n", 412 | " [-3.71766649e-02, 1.34489849e-01, 1.71046421e-01],\n", 413 | " [ 4.81249429e-02, -7.83700123e-02, -3.12581211e-02]],\n", 414 | "\n", 415 | " [[ 2.96847671e-02, -3.79320413e-01, -5.47845542e-01],\n", 416 | " [-4.33366634e-02, 5.34057677e-01, 6.51048243e-01],\n", 417 | " [ 3.82855870e-02, -1.92035049e-01, -1.35353908e-01]],\n", 418 | "\n", 419 | " [[ 5.45627736e-02, -2.13017032e-01, -2.65550435e-01],\n", 420 | " [-4.59416732e-02, 2.84033865e-01, 3.17390174e-01],\n", 421 | " [ 6.88189492e-02, -1.11302704e-01, -4.39153351e-02]]],\n", 422 | "\n", 423 | "\n", 424 | " [[[-2.12162770e-02, -1.06991991e-01, 2.17960645e-02],\n", 425 | " [-1.85261726e-01, -3.55821162e-01, -2.62350231e-01],\n", 426 | " [-1.62142962e-02, -2.01636493e-01, -1.62589252e-01]],\n", 427 | "\n", 428 | " [[-7.12832958e-02, -1.67650282e-01, -1.46480128e-01],\n", 429 | " [-1.25862047e-01, -1.79815277e-01, -1.78706363e-01],\n", 430 | " [-6.36041388e-02, -8.55675116e-02, -4.85080294e-02]],\n", 431 | "\n", 432 | " [[ 1.62563443e-01, 2.43959337e-01, 5.50123490e-02],\n", 433 | " [ 3.28164190e-01, 5.50043046e-01, 3.91553462e-01],\n", 434 | " [ 3.99634019e-02, 2.81825602e-01, 2.48636529e-01]]],\n", 435 | "\n", 436 | "\n", 437 | " [[[ 1.64480760e-34, 1.44052034e-34, -1.74678633e-34],\n", 438 | " [-1.63426202e-34, 1.52786715e-34, -1.29172652e-34],\n", 439 | " [ 1.66548253e-34, 1.29662549e-34, -1.62710264e-34]],\n", 440 | "\n", 441 | " [[ 1.02969333e-34, 1.23707177e-34, 1.27150664e-34],\n", 442 | " [ 1.32079303e-34, 9.88136278e-35, 1.20372206e-34],\n", 443 | " [ 1.08218581e-34, 1.54123266e-34, 1.40629291e-34]],\n", 444 | "\n", 445 | " [[ 1.67978786e-34, 1.16899205e-34, 1.27429408e-34],\n", 446 | " [-1.65518825e-33, 1.49328052e-34, 1.16677090e-34],\n", 447 | " [ 1.27539518e-34, -1.70589600e-34, 1.16419583e-34]]],\n", 448 | "\n", 449 | "\n", 450 | " [[[ 1.44589616e-34, -1.37115676e-34, 1.34690955e-34],\n", 451 | " [ 1.83407861e-34, -1.40984304e-34, -1.25344868e-34],\n", 452 | " [-1.47200534e-34, -1.19399128e-34, -1.50311611e-34]],\n", 453 | "\n", 454 | " [[ 1.50745315e-34, 1.32088199e-34, 1.51516492e-34],\n", 455 | " [-1.32816857e-34, 1.63055576e-34, -1.23988217e-34],\n", 456 | " [-1.48195847e-34, -1.08774542e-34, -1.29488669e-34]],\n", 457 | "\n", 458 | " [[ 1.17133076e-34, 1.36807683e-34, 1.39820829e-34],\n", 459 | " [-1.67508485e-34, 1.50253065e-34, -1.37729493e-34],\n", 460 | " [-1.24282240e-34, -1.35764305e-34, -1.69524262e-34]]],\n", 461 | "\n", 462 | "\n", 463 | " [[[-1.51967359e-34, 1.46410886e-34, -1.19750743e-34],\n", 464 | " [ 1.59706784e-34, -1.26047283e-34, -1.30651307e-34],\n", 465 | " [ 1.53955207e-34, -1.27950368e-34, -1.48564314e-34]],\n", 466 | "\n", 467 | " [[ 1.27417435e-34, 1.33416037e-34, 1.43654295e-34],\n", 468 | " [ 1.47722492e-34, 1.40092237e-34, -1.35528415e-34],\n", 469 | " [ 1.37652386e-34, -1.41398528e-34, 1.34853963e-34]],\n", 470 | "\n", 471 | " [[-1.50365908e-34, 1.56539469e-34, -1.64082309e-34],\n", 472 | " [-1.26958234e-34, 1.68977508e-34, -1.62103288e-34],\n", 473 | " [ 1.75427655e-34, -1.51688558e-34, -1.23453952e-34]]],\n", 474 | "\n", 475 | "\n", 476 | " [[[-1.04822870e-02, 1.78309262e-01, -1.72056288e-01],\n", 477 | " [ 1.48374774e-02, -2.87300594e-07, 1.44880614e-03],\n", 478 | " [ 8.48359521e-03, -2.01171324e-01, 1.79017499e-01]],\n", 479 | "\n", 480 | " [[ 6.41963780e-02, 5.56827545e-01, -5.93184948e-01],\n", 481 | " [ 3.11842114e-02, 1.77238565e-02, -1.60755757e-02],\n", 482 | " [-1.00300945e-01, -5.82468987e-01, 6.20574057e-01]],\n", 483 | "\n", 484 | " [[ 1.22758923e-02, 2.89336830e-01, -2.81411618e-01],\n", 485 | " [ 3.53562608e-02, 2.02878378e-02, -4.01876532e-02],\n", 486 | " [-3.31509486e-02, -3.11556220e-01, 3.14529926e-01]]],\n", 487 | "\n", 488 | "\n", 489 | " [[[-1.43254914e-34, -2.34915144e-33, 7.67188592e-34],\n", 490 | " [ 1.61831065e-34, -3.41576618e-34, -1.71331757e-34],\n", 491 | " [-1.80117624e-34, 1.39130662e-34, 1.37211828e-34]],\n", 492 | "\n", 493 | " [[-1.61246945e-34, -4.46681253e-35, -1.32352043e-34],\n", 494 | " [ 1.49444867e-34, 1.40735487e-34, -1.42098142e-34],\n", 495 | " [ 1.34009513e-34, -1.44926216e-34, 1.82309026e-34]],\n", 496 | "\n", 497 | " [[-1.71143184e-34, 1.50409530e-34, -1.52353378e-34],\n", 498 | " [ 1.38598016e-34, -1.35724380e-34, 1.52868793e-34],\n", 499 | " [-1.54150001e-34, 2.34914960e-33, -1.37733224e-34]]],\n", 500 | "\n", 501 | "\n", 502 | " [[[ 1.53773382e-01, 2.99308449e-01, 3.71925086e-01],\n", 503 | " [ 2.87195891e-01, 4.21678722e-01, 4.52245235e-01],\n", 504 | " [ 3.09944659e-01, 3.95957172e-01, 3.97120714e-01]],\n", 505 | "\n", 506 | " [[-2.66044378e-01, -4.22174662e-01, -4.24773335e-01],\n", 507 | " [-3.59487146e-01, -5.30575752e-01, -5.43352842e-01],\n", 508 | " [-3.39696467e-01, -5.14233291e-01, -4.81997162e-01]],\n", 509 | "\n", 510 | " [[ 1.49400979e-01, 9.64866206e-02, 5.91227859e-02],\n", 511 | " [ 8.51410627e-02, 7.98673406e-02, 9.34522450e-02],\n", 512 | " [ 2.21258700e-02, 5.91478460e-02, 1.14287823e-01]]],\n", 513 | "\n", 514 | "\n", 515 | " [[[ 3.16673815e-02, 1.24610784e-02, -2.52244361e-02],\n", 516 | " [-5.30945230e-03, 1.89187050e-01, -1.89831331e-01],\n", 517 | " [-1.87628355e-03, 2.95100752e-02, -3.45427021e-02]],\n", 518 | "\n", 519 | " [[-2.75633819e-02, 1.50377154e-01, -2.17702314e-01],\n", 520 | " [ 6.86713234e-02, 6.78148925e-01, -7.18262613e-01],\n", 521 | " [-2.74109915e-02, 2.24428862e-01, -2.51928180e-01]],\n", 522 | "\n", 523 | " [[-9.39995458e-04, 7.52176866e-02, -1.04380228e-01],\n", 524 | " [ 2.72466280e-02, 3.60943019e-01, -3.86763155e-01],\n", 525 | " [-1.06523540e-02, 1.31725580e-01, -1.35146096e-01]]]],\n", 526 | " dtype=float32)" 527 | ] 528 | }, 529 | "execution_count": 7, 530 | "metadata": {}, 531 | "output_type": "execute_result" 532 | } 533 | ], 534 | "source": [ 535 | "np.array(v.get_tensor())" 536 | ] 537 | } 538 | ], 539 | "metadata": { 540 | "kernelspec": { 541 | "display_name": "Python 2", 542 | "language": "python", 543 | "name": "python2" 544 | }, 545 | "language_info": { 546 | "codemirror_mode": { 547 | "name": "ipython", 548 | "version": 2 549 | }, 550 | "file_extension": ".py", 551 | "mimetype": "text/x-python", 552 | "name": "python", 553 | "nbconvert_exporter": "python", 554 | "pygments_lexer": "ipython2", 555 | "version": "2.7.15" 556 | } 557 | }, 558 | "nbformat": 4, 559 | "nbformat_minor": 2 560 | } 561 | -------------------------------------------------------------------------------- /paddle/pretrained.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import paddle.fluid as fluid\n", 10 | "import paddle\n", 11 | "import numpy as np\n", 12 | "import os\n", 13 | "import math\n", 14 | "from paddle.fluid.debugger import draw_block_graphviz" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 2, 20 | "metadata": {}, 21 | "outputs": [], 22 | "source": [ 23 | "# 预训练模型保存路径\n", 24 | "# 来自:https://github.com/PaddlePaddle/models/tree/develop/fluid/image_classification#supported-models-and-performances\n", 25 | "pretrained_model_path = \"se_resnext_50/129\"" 26 | ] 27 | }, 28 | { 29 | "cell_type": "code", 30 | "execution_count": 3, 31 | "metadata": {}, 32 | "outputs": [], 33 | "source": [ 34 | "# 根据program加载参数\n", 35 | "def load_pretrained_params(exe, program):\n", 36 | "\n", 37 | " # 只加载实际存在的\n", 38 | " def if_exist(var):\n", 39 | " path = os.path.join(pretrained_model_path, var.name)\n", 40 | " exist = os.path.exists(path)\n", 41 | " if exist:\n", 42 | " print(\"Load\", path)\n", 43 | " return exist\n", 44 | " \n", 45 | " fluid.io.load_vars(exe, pretrained_model_path, predicate=if_exist, main_program=program)" 46 | ] 47 | }, 48 | { 49 | "cell_type": "code", 50 | "execution_count": 4, 51 | "metadata": {}, 52 | "outputs": [], 53 | "source": [ 54 | "# 预训练模型定义,已去掉不需要的层\n", 55 | "# 来自:\n", 56 | "class SE_ResNeXt50():\n", 57 | " \n", 58 | " def __init__(self):\n", 59 | " self.variables = []\n", 60 | " \n", 61 | " def net(self, input, class_dim=1000):\n", 62 | " cardinality = 32\n", 63 | " reduction_ratio = 16\n", 64 | " depth = [3, 4, 6] #, 3]\n", 65 | " num_filters = [128, 256, 512, 1024]\n", 66 | " \n", 67 | " conv = self.conv_bn_layer(\n", 68 | " input=input,\n", 69 | " num_filters=64,\n", 70 | " filter_size=7,\n", 71 | " stride=2,\n", 72 | " act='relu')\n", 73 | " conv = fluid.layers.pool2d(\n", 74 | " input=conv,\n", 75 | " pool_size=3,\n", 76 | " pool_stride=2,\n", 77 | " pool_padding=1,\n", 78 | " pool_type='max')\n", 79 | " \n", 80 | " for block in range(len(depth)):\n", 81 | " for i in range(depth[block]):\n", 82 | " conv = self.bottleneck_block(\n", 83 | " input=conv,\n", 84 | " num_filters=num_filters[block],\n", 85 | " stride=2 if i == 0 and block != 0 else 1,\n", 86 | " cardinality=cardinality,\n", 87 | " reduction_ratio=reduction_ratio)\n", 88 | " pool = fluid.layers.pool2d( input=conv, pool_size=7, pool_type='avg', global_pooling=True) \n", 89 | " return pool\n", 90 | "\n", 91 | " def shortcut(self, input, ch_out, stride):\n", 92 | " ch_in = input.shape[1]\n", 93 | " if ch_in != ch_out or stride != 1:\n", 94 | " filter_size = 1\n", 95 | " return self.conv_bn_layer(input, ch_out, filter_size, stride)\n", 96 | " else:\n", 97 | " return input\n", 98 | "\n", 99 | " def bottleneck_block(self, input, num_filters, stride, cardinality,\n", 100 | " reduction_ratio):\n", 101 | " conv0 = self.conv_bn_layer(\n", 102 | " input=input, num_filters=num_filters, filter_size=1, act='relu')\n", 103 | " conv1 = self.conv_bn_layer(\n", 104 | " input=conv0,\n", 105 | " num_filters=num_filters,\n", 106 | " filter_size=3,\n", 107 | " stride=stride,\n", 108 | " groups=cardinality,\n", 109 | " act='relu')\n", 110 | " conv2 = self.conv_bn_layer(\n", 111 | " input=conv1, num_filters=num_filters * 2, filter_size=1, act=None)\n", 112 | " scale = self.squeeze_excitation(\n", 113 | " input=conv2,\n", 114 | " num_channels=num_filters * 2,\n", 115 | " reduction_ratio=reduction_ratio)\n", 116 | "\n", 117 | " short = self.shortcut(input, num_filters * 2, stride)\n", 118 | "\n", 119 | " return fluid.layers.elementwise_add(x=short, y=scale, act='relu')\n", 120 | "\n", 121 | " def conv_bn_layer(self,\n", 122 | " input,\n", 123 | " num_filters,\n", 124 | " filter_size,\n", 125 | " stride=1,\n", 126 | " groups=1,\n", 127 | " act=None):\n", 128 | " conv = fluid.layers.conv2d(\n", 129 | " input=input,\n", 130 | " num_filters=num_filters,\n", 131 | " filter_size=filter_size,\n", 132 | " stride=stride,\n", 133 | " padding=(filter_size - 1) / 2,\n", 134 | " groups=groups,\n", 135 | " act=None,\n", 136 | " bias_attr=False)\n", 137 | " self.variables.append(conv)\n", 138 | " bn = fluid.layers.batch_norm(input=conv, act=act)\n", 139 | " self.variables.append(bn)\n", 140 | " return bn\n", 141 | "\n", 142 | " def squeeze_excitation(self, input, num_channels, reduction_ratio):\n", 143 | " pool = fluid.layers.pool2d(\n", 144 | " input=input, pool_size=0, pool_type='avg', global_pooling=True)\n", 145 | " stdv = 1.0 / math.sqrt(pool.shape[1] * 1.0)\n", 146 | " squeeze = fluid.layers.fc(input=pool,\n", 147 | " size=num_channels / reduction_ratio,\n", 148 | " act='relu',\n", 149 | " param_attr=fluid.param_attr.ParamAttr(\n", 150 | " initializer=fluid.initializer.Uniform(\n", 151 | " -stdv, stdv)))\n", 152 | " self.variables.append(squeeze)\n", 153 | " stdv = 1.0 / math.sqrt(squeeze.shape[1] * 1.0)\n", 154 | " excitation = fluid.layers.fc(input=squeeze,\n", 155 | " size=num_channels,\n", 156 | " act='sigmoid',\n", 157 | " param_attr=fluid.param_attr.ParamAttr(\n", 158 | " initializer=fluid.initializer.Uniform(\n", 159 | " -stdv, stdv)))\n", 160 | " self.variables.append(excitation)\n", 161 | " scale = fluid.layers.elementwise_mul(x=input, y=excitation, axis=0)\n", 162 | " return scale" 163 | ] 164 | }, 165 | { 166 | "cell_type": "code", 167 | "execution_count": 5, 168 | "metadata": {}, 169 | "outputs": [], 170 | "source": [ 171 | "# 自定义模型\n", 172 | "def network(image, train_base_model=False):\n", 173 | " \n", 174 | " # 预训练模型\n", 175 | " base_model = SE_ResNeXt50().net(image)\n", 176 | " # 控制是否训练base_model\n", 177 | " base_model.stop_gradient = not train_base_model \n", 178 | " \n", 179 | " # 复制一个只包含base_model的program,放便只加载需要的参数\n", 180 | " base_model_program = fluid.default_main_program().clone() \n", 181 | " \n", 182 | " # 添加新的层\n", 183 | " y = base_model\n", 184 | " y = fluid.layers.fc(base_model, size=10, act='softmax')\n", 185 | " \n", 186 | " return y, base_model_program" 187 | ] 188 | }, 189 | { 190 | "cell_type": "code", 191 | "execution_count": 6, 192 | "metadata": {}, 193 | "outputs": [], 194 | "source": [ 195 | "use_cuda = fluid.core.is_compiled_with_cuda()\n", 196 | "place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()" 197 | ] 198 | }, 199 | { 200 | "cell_type": "code", 201 | "execution_count": 7, 202 | "metadata": { 203 | "scrolled": true 204 | }, 205 | "outputs": [ 206 | { 207 | "name": "stdout", 208 | "output_type": "stream", 209 | "text": [ 210 | "('Load', 'se_resnext_50/129/batch_norm_10.w_1')\n", 211 | "('Load', 'se_resnext_50/129/batch_norm_33.w_2')\n", 212 | "('Load', 'se_resnext_50/129/batch_norm_33.w_0')\n", 213 | "('Load', 'se_resnext_50/129/conv2d_33.w_0')\n", 214 | "('Load', 'se_resnext_50/129/batch_norm_17.w_1')\n", 215 | "('Load', 'se_resnext_50/129/batch_norm_32.w_0')\n", 216 | "('Load', 'se_resnext_50/129/batch_norm_31.w_0')\n", 217 | "('Load', 'se_resnext_50/129/batch_norm_28.b_0')\n", 218 | "('Load', 'se_resnext_50/129/batch_norm_25.b_0')\n", 219 | "('Load', 'se_resnext_50/129/batch_norm_31.w_2')\n", 220 | "('Load', 'se_resnext_50/129/batch_norm_31.w_1')\n", 221 | "('Load', 'se_resnext_50/129/batch_norm_26.w_0')\n", 222 | "('Load', 'se_resnext_50/129/batch_norm_31.b_0')\n", 223 | "('Load', 'se_resnext_50/129/conv2d_31.w_0')\n", 224 | "('Load', 'se_resnext_50/129/fc_17.w_0')\n", 225 | "('Load', 'se_resnext_50/129/fc_16.b_0')\n", 226 | "('Load', 'se_resnext_50/129/fc_16.w_0')\n", 227 | "('Load', 'se_resnext_50/129/fc_8.w_0')\n", 228 | "('Load', 'se_resnext_50/129/batch_norm_42.w_2')\n", 229 | "('Load', 'se_resnext_50/129/batch_norm_30.w_1')\n", 230 | "('Load', 'se_resnext_50/129/conv2d_1.w_0')\n", 231 | "('Load', 'se_resnext_50/129/conv2d_30.w_0')\n", 232 | "('Load', 'se_resnext_50/129/batch_norm_35.w_2')\n", 233 | "('Load', 'se_resnext_50/129/batch_norm_36.b_0')\n", 234 | "('Load', 'se_resnext_50/129/batch_norm_29.w_2')\n", 235 | "('Load', 'se_resnext_50/129/fc_24.w_0')\n", 236 | "('Load', 'se_resnext_50/129/batch_norm_29.w_1')\n", 237 | "('Load', 'se_resnext_50/129/batch_norm_29.b_0')\n", 238 | "('Load', 'se_resnext_50/129/fc_24.b_0')\n", 239 | "('Load', 'se_resnext_50/129/batch_norm_28.w_0')\n", 240 | "('Load', 'se_resnext_50/129/batch_norm_27.w_1')\n", 241 | "('Load', 'se_resnext_50/129/fc_14.w_0')\n", 242 | "('Load', 'se_resnext_50/129/batch_norm_27.b_0')\n", 243 | "('Load', 'se_resnext_50/129/batch_norm_5.b_0')\n", 244 | "('Load', 'se_resnext_50/129/batch_norm_27.w_0')\n", 245 | "('Load', 'se_resnext_50/129/batch_norm_19.w_0')\n", 246 | "('Load', 'se_resnext_50/129/batch_norm_37.w_0')\n", 247 | "('Load', 'se_resnext_50/129/conv2d_27.w_0')\n", 248 | "('Load', 'se_resnext_50/129/fc_15.w_0')\n", 249 | "('Load', 'se_resnext_50/129/batch_norm_12.b_0')\n", 250 | "('Load', 'se_resnext_50/129/batch_norm_34.w_0')\n", 251 | "('Load', 'se_resnext_50/129/conv2d_18.w_0')\n", 252 | "('Load', 'se_resnext_50/129/conv2d_26.w_0')\n", 253 | "('Load', 'se_resnext_50/129/batch_norm_1.b_0')\n", 254 | "('Load', 'se_resnext_50/129/batch_norm_25.w_1')\n", 255 | "('Load', 'se_resnext_50/129/conv2d_17.w_0')\n", 256 | "('Load', 'se_resnext_50/129/batch_norm_38.w_0')\n", 257 | "('Load', 'se_resnext_50/129/fc_15.b_0')\n", 258 | "('Load', 'se_resnext_50/129/batch_norm_26.w_1')\n", 259 | "('Load', 'se_resnext_50/129/batch_norm_24.w_2')\n", 260 | "('Load', 'se_resnext_50/129/batch_norm_28.w_2')\n", 261 | "('Load', 'se_resnext_50/129/fc_13.w_0')\n", 262 | "('Load', 'se_resnext_50/129/batch_norm_36.w_1')\n", 263 | "('Load', 'se_resnext_50/129/batch_norm_32.w_2')\n", 264 | "('Load', 'se_resnext_50/129/fc_12.b_0')\n", 265 | "('Load', 'se_resnext_50/129/batch_norm_23.w_2')\n", 266 | "('Load', 'se_resnext_50/129/batch_norm_23.b_0')\n", 267 | "('Load', 'se_resnext_50/129/batch_norm_23.w_0')\n", 268 | "('Load', 'se_resnext_50/129/conv2d_24.w_0')\n", 269 | "('Load', 'se_resnext_50/129/fc_21.b_0')\n", 270 | "('Load', 'se_resnext_50/129/fc_17.b_0')\n", 271 | "('Load', 'se_resnext_50/129/batch_norm_22.w_2')\n", 272 | "('Load', 'se_resnext_50/129/fc_10.w_0')\n", 273 | "('Load', 'se_resnext_50/129/batch_norm_30.b_0')\n", 274 | "('Load', 'se_resnext_50/129/batch_norm_37.w_2')\n", 275 | "('Load', 'se_resnext_50/129/batch_norm_22.w_1')\n", 276 | "('Load', 'se_resnext_50/129/batch_norm_9.w_1')\n", 277 | "('Load', 'se_resnext_50/129/batch_norm_39.w_1')\n", 278 | "('Load', 'se_resnext_50/129/batch_norm_1.w_1')\n", 279 | "('Load', 'se_resnext_50/129/batch_norm_20.w_0')\n", 280 | "('Load', 'se_resnext_50/129/batch_norm_42.w_0')\n", 281 | "('Load', 'se_resnext_50/129/batch_norm_20.w_1')\n", 282 | "('Load', 'se_resnext_50/129/batch_norm_2.w_1')\n", 283 | "('Load', 'se_resnext_50/129/batch_norm_6.w_2')\n", 284 | "('Load', 'se_resnext_50/129/batch_norm_19.w_2')\n", 285 | "('Load', 'se_resnext_50/129/batch_norm_5.w_1')\n", 286 | "('Load', 'se_resnext_50/129/batch_norm_8.w_0')\n", 287 | "('Load', 'se_resnext_50/129/conv2d_9.w_0')\n", 288 | "('Load', 'se_resnext_50/129/fc_8.b_0')\n", 289 | "('Load', 'se_resnext_50/129/batch_norm_11.w_2')\n", 290 | "('Load', 'se_resnext_50/129/batch_norm_5.w_2')\n", 291 | "('Load', 'se_resnext_50/129/conv2d_10.w_0')\n", 292 | "('Load', 'se_resnext_50/129/batch_norm_37.w_1')\n", 293 | "('Load', 'se_resnext_50/129/batch_norm_10.w_0')\n", 294 | "('Load', 'se_resnext_50/129/batch_norm_34.w_2')\n", 295 | "('Load', 'se_resnext_50/129/batch_norm_15.b_0')\n", 296 | "('Load', 'se_resnext_50/129/batch_norm_8.w_2')\n", 297 | "('Load', 'se_resnext_50/129/fc_18.b_0')\n", 298 | "('Load', 'se_resnext_50/129/fc_5.w_0')\n", 299 | "('Load', 'se_resnext_50/129/batch_norm_9.b_0')\n", 300 | "('Load', 'se_resnext_50/129/conv2d_37.w_0')\n", 301 | "('Load', 'se_resnext_50/129/batch_norm_38.b_0')\n", 302 | "('Load', 'se_resnext_50/129/batch_norm_0.w_1')\n", 303 | "('Load', 'se_resnext_50/129/fc_1.w_0')\n", 304 | "('Load', 'se_resnext_50/129/fc_2.w_0')\n", 305 | "('Load', 'se_resnext_50/129/batch_norm_35.b_0')\n", 306 | "('Load', 'se_resnext_50/129/conv2d_15.w_0')\n", 307 | "('Load', 'se_resnext_50/129/batch_norm_24.w_1')\n", 308 | "('Load', 'se_resnext_50/129/batch_norm_12.w_0')\n", 309 | "('Load', 'se_resnext_50/129/batch_norm_16.b_0')\n", 310 | "('Load', 'se_resnext_50/129/conv2d_32.w_0')\n", 311 | "('Load', 'se_resnext_50/129/conv2d_34.w_0')\n", 312 | "('Load', 'se_resnext_50/129/batch_norm_12.w_1')\n", 313 | "('Load', 'se_resnext_50/129/batch_norm_2.w_2')\n", 314 | "('Load', 'se_resnext_50/129/fc_18.w_0')\n", 315 | "('Load', 'se_resnext_50/129/fc_6.w_0')\n", 316 | "('Load', 'se_resnext_50/129/batch_norm_6.b_0')\n", 317 | "('Load', 'se_resnext_50/129/batch_norm_12.w_2')\n", 318 | "('Load', 'se_resnext_50/129/fc_19.b_0')\n", 319 | "('Load', 'se_resnext_50/129/conv2d_13.w_0')\n", 320 | "('Load', 'se_resnext_50/129/batch_norm_38.w_1')\n", 321 | "('Load', 'se_resnext_50/129/fc_7.w_0')\n", 322 | "('Load', 'se_resnext_50/129/batch_norm_26.w_2')\n", 323 | "('Load', 'se_resnext_50/129/conv2d_28.w_0')\n", 324 | "('Load', 'se_resnext_50/129/batch_norm_40.w_0')\n", 325 | "('Load', 'se_resnext_50/129/batch_norm_41.w_0')\n", 326 | "('Load', 'se_resnext_50/129/batch_norm_13.b_0')\n", 327 | "('Load', 'se_resnext_50/129/fc_1.b_0')\n", 328 | "('Load', 'se_resnext_50/129/batch_norm_32.b_0')\n", 329 | "('Load', 'se_resnext_50/129/batch_norm_10.b_0')\n", 330 | "('Load', 'se_resnext_50/129/batch_norm_38.w_2')\n", 331 | "('Load', 'se_resnext_50/129/batch_norm_18.w_1')\n", 332 | "('Load', 'se_resnext_50/129/conv2d_11.w_0')\n", 333 | "('Load', 'se_resnext_50/129/conv2d_0.w_0')\n", 334 | "('Load', 'se_resnext_50/129/batch_norm_30.w_0')\n", 335 | "('Load', 'se_resnext_50/129/batch_norm_13.w_1')\n", 336 | "('Load', 'se_resnext_50/129/batch_norm_8.b_0')\n", 337 | "('Load', 'se_resnext_50/129/fc_23.b_0')\n", 338 | "('Load', 'se_resnext_50/129/conv2d_7.w_0')\n", 339 | "('Load', 'se_resnext_50/129/fc_12.w_0')\n", 340 | "('Load', 'se_resnext_50/129/batch_norm_11.b_0')\n", 341 | "('Load', 'se_resnext_50/129/batch_norm_15.w_2')\n", 342 | "('Load', 'se_resnext_50/129/batch_norm_10.w_2')\n", 343 | "('Load', 'se_resnext_50/129/batch_norm_42.b_0')\n", 344 | "('Load', 'se_resnext_50/129/batch_norm_42.w_1')\n", 345 | "('Load', 'se_resnext_50/129/batch_norm_39.b_0')\n", 346 | "('Load', 'se_resnext_50/129/fc_10.b_0')\n", 347 | "('Load', 'se_resnext_50/129/batch_norm_39.w_0')\n", 348 | "('Load', 'se_resnext_50/129/fc_20.b_0')\n", 349 | "('Load', 'se_resnext_50/129/fc_14.b_0')\n", 350 | "('Load', 'se_resnext_50/129/fc_25.w_0')\n", 351 | "('Load', 'se_resnext_50/129/batch_norm_20.w_2')\n", 352 | "('Load', 'se_resnext_50/129/batch_norm_3.b_0')\n", 353 | "('Load', 'se_resnext_50/129/fc_21.w_0')\n", 354 | "('Load', 'se_resnext_50/129/fc_4.b_0')\n", 355 | "('Load', 'se_resnext_50/129/batch_norm_33.b_0')\n", 356 | "('Load', 'se_resnext_50/129/batch_norm_1.w_0')\n", 357 | "('Load', 'se_resnext_50/129/fc_25.b_0')\n", 358 | "('Load', 'se_resnext_50/129/conv2d_21.w_0')\n", 359 | "('Load', 'se_resnext_50/129/conv2d_38.w_0')\n", 360 | "('Load', 'se_resnext_50/129/conv2d_40.w_0')\n", 361 | "('Load', 'se_resnext_50/129/conv2d_29.w_0')\n", 362 | "('Load', 'se_resnext_50/129/conv2d_41.w_0')\n", 363 | "('Load', 'se_resnext_50/129/batch_norm_4.w_1')\n", 364 | "('Load', 'se_resnext_50/129/batch_norm_40.w_1')\n", 365 | "('Load', 'se_resnext_50/129/batch_norm_40.b_0')\n", 366 | "('Load', 'se_resnext_50/129/batch_norm_35.w_1')\n", 367 | "('Load', 'se_resnext_50/129/batch_norm_39.w_2')\n", 368 | "('Load', 'se_resnext_50/129/fc_22.w_0')\n", 369 | "('Load', 'se_resnext_50/129/batch_norm_41.w_2')\n", 370 | "('Load', 'se_resnext_50/129/fc_0.w_0')\n", 371 | "('Load', 'se_resnext_50/129/conv2d_36.w_0')\n", 372 | "('Load', 'se_resnext_50/129/batch_norm_0.w_2')\n", 373 | "('Load', 'se_resnext_50/129/batch_norm_23.w_1')\n", 374 | "('Load', 'se_resnext_50/129/batch_norm_40.w_2')\n", 375 | "('Load', 'se_resnext_50/129/batch_norm_13.w_2')\n", 376 | "('Load', 'se_resnext_50/129/batch_norm_8.w_1')\n", 377 | "('Load', 'se_resnext_50/129/batch_norm_36.w_2')\n", 378 | "('Load', 'se_resnext_50/129/batch_norm_5.w_0')\n", 379 | "('Load', 'se_resnext_50/129/conv2d_39.w_0')\n", 380 | "('Load', 'se_resnext_50/129/batch_norm_6.w_0')\n", 381 | "('Load', 'se_resnext_50/129/batch_norm_13.w_0')\n", 382 | "('Load', 'se_resnext_50/129/batch_norm_35.w_0')\n", 383 | "('Load', 'se_resnext_50/129/conv2d_6.w_0')\n", 384 | "('Load', 'se_resnext_50/129/fc_4.w_0')\n", 385 | "('Load', 'se_resnext_50/129/fc_6.b_0')\n", 386 | "('Load', 'se_resnext_50/129/conv2d_20.w_0')\n", 387 | "('Load', 'se_resnext_50/129/batch_norm_14.w_0')\n", 388 | "('Load', 'se_resnext_50/129/fc_23.w_0')\n", 389 | "('Load', 'se_resnext_50/129/conv2d_12.w_0')\n", 390 | "('Load', 'se_resnext_50/129/batch_norm_32.w_1')\n", 391 | "('Load', 'se_resnext_50/129/batch_norm_20.b_0')\n", 392 | "('Load', 'se_resnext_50/129/batch_norm_17.w_2')\n", 393 | "('Load', 'se_resnext_50/129/batch_norm_15.w_0')\n", 394 | "('Load', 'se_resnext_50/129/batch_norm_16.w_1')\n", 395 | "('Load', 'se_resnext_50/129/batch_norm_1.w_2')\n", 396 | "('Load', 'se_resnext_50/129/batch_norm_0.b_0')\n", 397 | "('Load', 'se_resnext_50/129/conv2d_16.w_0')\n", 398 | "('Load', 'se_resnext_50/129/batch_norm_16.w_2')\n", 399 | "('Load', 'se_resnext_50/129/batch_norm_7.w_0')\n", 400 | "('Load', 'se_resnext_50/129/batch_norm_15.w_1')\n", 401 | "('Load', 'se_resnext_50/129/batch_norm_11.w_0')\n", 402 | "('Load', 'se_resnext_50/129/fc_5.b_0')\n", 403 | "('Load', 'se_resnext_50/129/batch_norm_7.w_2')\n", 404 | "('Load', 'se_resnext_50/129/conv2d_42.w_0')\n", 405 | "('Load', 'se_resnext_50/129/batch_norm_3.w_1')\n", 406 | "('Load', 'se_resnext_50/129/batch_norm_2.w_0')\n", 407 | "('Load', 'se_resnext_50/129/batch_norm_34.b_0')\n", 408 | "('Load', 'se_resnext_50/129/batch_norm_21.w_0')\n", 409 | "('Load', 'se_resnext_50/129/fc_13.b_0')\n", 410 | "('Load', 'se_resnext_50/129/batch_norm_37.b_0')\n", 411 | "('Load', 'se_resnext_50/129/batch_norm_16.w_0')\n", 412 | "('Load', 'se_resnext_50/129/fc_20.w_0')\n", 413 | "('Load', 'se_resnext_50/129/batch_norm_3.w_2')\n", 414 | "('Load', 'se_resnext_50/129/batch_norm_33.w_1')\n", 415 | "('Load', 'se_resnext_50/129/fc_11.b_0')\n", 416 | "('Load', 'se_resnext_50/129/batch_norm_14.b_0')\n", 417 | "('Load', 'se_resnext_50/129/conv2d_25.w_0')\n", 418 | "('Load', 'se_resnext_50/129/batch_norm_9.w_0')\n", 419 | "('Load', 'se_resnext_50/129/batch_norm_11.w_1')\n", 420 | "('Load', 'se_resnext_50/129/batch_norm_4.w_2')\n", 421 | "('Load', 'se_resnext_50/129/batch_norm_3.w_0')\n", 422 | "('Load', 'se_resnext_50/129/fc_7.b_0')\n", 423 | "('Load', 'se_resnext_50/129/batch_norm_4.w_0')\n", 424 | "('Load', 'se_resnext_50/129/conv2d_5.w_0')\n", 425 | "('Load', 'se_resnext_50/129/batch_norm_4.b_0')\n", 426 | "('Load', 'se_resnext_50/129/batch_norm_17.w_0')\n", 427 | "('Load', 'se_resnext_50/129/batch_norm_21.b_0')\n", 428 | "('Load', 'se_resnext_50/129/fc_3.b_0')\n", 429 | "('Load', 'se_resnext_50/129/conv2d_8.w_0')\n", 430 | "('Load', 'se_resnext_50/129/batch_norm_14.w_2')\n", 431 | "('Load', 'se_resnext_50/129/conv2d_3.w_0')\n", 432 | "('Load', 'se_resnext_50/129/conv2d_22.w_0')\n", 433 | "('Load', 'se_resnext_50/129/fc_9.w_0')\n", 434 | "('Load', 'se_resnext_50/129/batch_norm_9.w_2')\n", 435 | "('Load', 'se_resnext_50/129/batch_norm_0.w_0')\n", 436 | "('Load', 'se_resnext_50/129/batch_norm_6.w_1')\n", 437 | "('Load', 'se_resnext_50/129/batch_norm_7.b_0')\n", 438 | "('Load', 'se_resnext_50/129/batch_norm_2.b_0')\n", 439 | "('Load', 'se_resnext_50/129/fc_3.w_0')\n", 440 | "('Load', 'se_resnext_50/129/batch_norm_7.w_1')\n", 441 | "('Load', 'se_resnext_50/129/batch_norm_29.w_0')\n", 442 | "('Load', 'se_resnext_50/129/batch_norm_34.w_1')\n", 443 | "('Load', 'se_resnext_50/129/batch_norm_36.w_0')\n", 444 | "('Load', 'se_resnext_50/129/batch_norm_14.w_1')\n", 445 | "('Load', 'se_resnext_50/129/batch_norm_25.w_2')\n", 446 | "('Load', 'se_resnext_50/129/fc_0.b_0')\n", 447 | "('Load', 'se_resnext_50/129/fc_2.b_0')\n", 448 | "('Load', 'se_resnext_50/129/conv2d_35.w_0')\n", 449 | "('Load', 'se_resnext_50/129/batch_norm_17.b_0')\n", 450 | "('Load', 'se_resnext_50/129/conv2d_2.w_0')\n", 451 | "('Load', 'se_resnext_50/129/batch_norm_25.w_0')\n", 452 | "('Load', 'se_resnext_50/129/conv2d_23.w_0')\n", 453 | "('Load', 'se_resnext_50/129/batch_norm_24.b_0')\n", 454 | "('Load', 'se_resnext_50/129/batch_norm_27.w_2')\n", 455 | "('Load', 'se_resnext_50/129/fc_9.b_0')\n", 456 | "('Load', 'se_resnext_50/129/batch_norm_30.w_2')\n", 457 | "('Load', 'se_resnext_50/129/batch_norm_18.w_0')\n", 458 | "('Load', 'se_resnext_50/129/batch_norm_18.b_0')\n", 459 | "('Load', 'se_resnext_50/129/batch_norm_18.w_2')\n", 460 | "('Load', 'se_resnext_50/129/batch_norm_21.w_2')\n", 461 | "('Load', 'se_resnext_50/129/conv2d_19.w_0')\n", 462 | "('Load', 'se_resnext_50/129/batch_norm_28.w_1')\n", 463 | "('Load', 'se_resnext_50/129/fc_19.w_0')\n", 464 | "('Load', 'se_resnext_50/129/batch_norm_19.b_0')\n", 465 | "('Load', 'se_resnext_50/129/batch_norm_19.w_1')\n", 466 | "('Load', 'se_resnext_50/129/fc_22.b_0')\n", 467 | "('Load', 'se_resnext_50/129/conv2d_4.w_0')\n", 468 | "('Load', 'se_resnext_50/129/conv2d_14.w_0')\n", 469 | "('Load', 'se_resnext_50/129/batch_norm_26.b_0')\n", 470 | "('Load', 'se_resnext_50/129/batch_norm_41.b_0')\n", 471 | "('Load', 'se_resnext_50/129/fc_11.w_0')\n", 472 | "('Load', 'se_resnext_50/129/batch_norm_21.w_1')\n", 473 | "('Load', 'se_resnext_50/129/batch_norm_24.w_0')\n", 474 | "('Load', 'se_resnext_50/129/batch_norm_22.w_0')\n", 475 | "('Load', 'se_resnext_50/129/batch_norm_41.w_1')\n", 476 | "('Load', 'se_resnext_50/129/batch_norm_22.b_0')\n" 477 | ] 478 | }, 479 | { 480 | "name": "stdout", 481 | "output_type": "stream", 482 | "text": [ 483 | "Epoch 0 Batch 0 Loss 2.311845 Acc 0.054688\n", 484 | "Epoch 0 Batch 100 Loss 2.132499 Acc 0.453125\n", 485 | "Epoch 0 Batch 200 Loss 1.953558 Acc 0.539062\n", 486 | "Epoch 0 Batch 300 Loss 1.840348 Acc 0.546875\n", 487 | "Epoch 1 Batch 0 Loss 1.777899 Acc 0.515625\n", 488 | "Epoch 1 Batch 100 Loss 1.705209 Acc 0.609375\n", 489 | "Epoch 1 Batch 200 Loss 1.572993 Acc 0.570312\n", 490 | "Epoch 1 Batch 300 Loss 1.525481 Acc 0.578125\n", 491 | "Epoch 2 Batch 0 Loss 1.530093 Acc 0.539062\n", 492 | "Epoch 2 Batch 100 Loss 1.480704 Acc 0.632812\n", 493 | "Epoch 2 Batch 200 Loss 1.379631 Acc 0.593750\n", 494 | "Epoch 2 Batch 300 Loss 1.353684 Acc 0.593750\n", 495 | "Epoch 3 Batch 0 Loss 1.393383 Acc 0.546875\n", 496 | "Epoch 3 Batch 100 Loss 1.345412 Acc 0.625000\n", 497 | "Epoch 3 Batch 200 Loss 1.266868 Acc 0.593750\n", 498 | "Epoch 3 Batch 300 Loss 1.247394 Acc 0.625000\n", 499 | "Epoch 4 Batch 0 Loss 1.308322 Acc 0.570312\n", 500 | "Epoch 4 Batch 100 Loss 1.255022 Acc 0.640625\n", 501 | "Epoch 4 Batch 200 Loss 1.193787 Acc 0.601562\n", 502 | "Epoch 4 Batch 300 Loss 1.174929 Acc 0.640625\n", 503 | "Epoch 5 Batch 0 Loss 1.250353 Acc 0.593750\n", 504 | "Epoch 5 Batch 100 Loss 1.189938 Acc 0.656250\n", 505 | "Epoch 5 Batch 200 Loss 1.142630 Acc 0.609375\n", 506 | "Epoch 5 Batch 300 Loss 1.121977 Acc 0.648438\n", 507 | "Epoch 6 Batch 0 Loss 1.208088 Acc 0.593750\n", 508 | "Epoch 6 Batch 100 Loss 1.140468 Acc 0.656250\n", 509 | "Epoch 6 Batch 200 Loss 1.104744 Acc 0.632812\n", 510 | "Epoch 6 Batch 300 Loss 1.081330 Acc 0.656250\n", 511 | "Epoch 7 Batch 0 Loss 1.175674 Acc 0.601562\n", 512 | "Epoch 7 Batch 100 Loss 1.101330 Acc 0.664062\n", 513 | "Epoch 7 Batch 200 Loss 1.075476 Acc 0.648438\n", 514 | "Epoch 7 Batch 300 Loss 1.048988 Acc 0.671875\n", 515 | "Epoch 8 Batch 0 Loss 1.149839 Acc 0.609375\n", 516 | "Epoch 8 Batch 100 Loss 1.069414 Acc 0.656250\n", 517 | "Epoch 8 Batch 200 Loss 1.052117 Acc 0.656250\n", 518 | "Epoch 8 Batch 300 Loss 1.022541 Acc 0.679688\n", 519 | "Epoch 9 Batch 0 Loss 1.128621 Acc 0.625000\n", 520 | "Epoch 9 Batch 100 Loss 1.042766 Acc 0.664062\n", 521 | "Epoch 9 Batch 200 Loss 1.032989 Acc 0.664062\n", 522 | "Epoch 9 Batch 300 Loss 1.000447 Acc 0.679688\n" 523 | ] 524 | } 525 | ], 526 | "source": [ 527 | "# 训练新加的层\n", 528 | "main = fluid.Program()\n", 529 | "startup = fluid.Program()\n", 530 | "exe = fluid.Executor(place)\n", 531 | "\n", 532 | "with fluid.unique_name.guard():\n", 533 | " with fluid.program_guard(main, startup):\n", 534 | "\n", 535 | " # 预测\n", 536 | " image = fluid.layers.data('image', [3, 32, 32], dtype='float32')\n", 537 | " predict, base_model_program = network(image, False) # 只训练新加的层\n", 538 | " \n", 539 | " # 损失\n", 540 | " label = fluid.layers.data('label', [1], dtype='int64')\n", 541 | " loss = fluid.layers.cross_entropy(label=label, input=predict)\n", 542 | " loss = fluid.layers.mean(loss)\n", 543 | " \n", 544 | " # Metric\n", 545 | " acc = fluid.layers.accuracy(predict, label)\n", 546 | " \n", 547 | " # Feeder\n", 548 | " feeder = fluid.data_feeder.DataFeeder([image, label], place)\n", 549 | " reader = feeder.decorate_reader(paddle.batch(paddle.dataset.cifar.train10(), 128), None)\n", 550 | "\n", 551 | " # opt\n", 552 | " optimizer = fluid.optimizer.Adam(learning_rate=0.001)\n", 553 | " optimizer.minimize(loss)\n", 554 | "\n", 555 | " # 初始化变量\n", 556 | " exe.run(startup)\n", 557 | "\n", 558 | " # 加载参数\n", 559 | " load_pretrained_params(exe, base_model_program)\n", 560 | "\n", 561 | " optimized_main = fluid.transpiler.memory_optimize(main, print_log=False)\n", 562 | "\n", 563 | " \n", 564 | " for epoch in range(10):\n", 565 | " for batch, data in enumerate(reader()):\n", 566 | " result = exe.run(fetch_list=[loss, acc], feed=data)\n", 567 | " if batch % 100 == 0:\n", 568 | " print(\"Epoch %d Batch %d Loss %f Acc %f\" % (epoch, batch, result[0][0], result[1][0]))" 569 | ] 570 | }, 571 | { 572 | "cell_type": "code", 573 | "execution_count": 8, 574 | "metadata": {}, 575 | "outputs": [], 576 | "source": [ 577 | "# 保存全部网络参数\n", 578 | "fluid.io.save_params(exe, \"cifar10.model\", main_program=main)" 579 | ] 580 | }, 581 | { 582 | "cell_type": "code", 583 | "execution_count": null, 584 | "metadata": {}, 585 | "outputs": [ 586 | { 587 | "name": "stdout", 588 | "output_type": "stream", 589 | "text": [ 590 | "Epoch 0 Batch 0 Loss 1.110775 Acc 0.640625\n", 591 | "Epoch 0 Batch 100 Loss 0.675765 Acc 0.781250\n", 592 | "Epoch 0 Batch 200 Loss 0.561026 Acc 0.804688\n", 593 | "Epoch 0 Batch 300 Loss 0.399023 Acc 0.843750\n", 594 | "Epoch 0 Test Acc 0.838241\n", 595 | "Epoch 1 Batch 0 Loss 0.339029 Acc 0.875000\n", 596 | "Epoch 1 Batch 100 Loss 0.324468 Acc 0.875000\n", 597 | "Epoch 1 Batch 200 Loss 0.180062 Acc 0.914062\n", 598 | "Epoch 1 Batch 300 Loss 0.110104 Acc 0.968750\n", 599 | "Epoch 1 Test Acc 0.843349\n", 600 | "Epoch 2 Batch 0 Loss 0.078147 Acc 0.992188\n", 601 | "Epoch 2 Batch 100 Loss 0.078254 Acc 0.976562\n", 602 | "Epoch 2 Batch 200 Loss 0.013229 Acc 1.000000\n", 603 | "Epoch 2 Batch 300 Loss 0.021719 Acc 0.992188\n", 604 | "Epoch 2 Test Acc 0.846955\n", 605 | "Epoch 3 Batch 0 Loss 0.007396 Acc 1.000000\n", 606 | "Epoch 3 Batch 100 Loss 0.008246 Acc 1.000000\n", 607 | "Epoch 3 Batch 200 Loss 0.012331 Acc 1.000000\n", 608 | "Epoch 3 Batch 300 Loss 0.010200 Acc 1.000000\n", 609 | "Epoch 3 Test Acc 0.844050\n", 610 | "Epoch 4 Batch 0 Loss 0.034095 Acc 0.976562\n", 611 | "Epoch 4 Batch 100 Loss 0.043675 Acc 0.984375\n", 612 | "Epoch 4 Batch 200 Loss 0.059676 Acc 0.984375\n", 613 | "Epoch 4 Batch 300 Loss 0.028524 Acc 1.000000\n", 614 | "Epoch 4 Test Acc 0.842849\n", 615 | "Epoch 5 Batch 0 Loss 0.055586 Acc 0.968750\n", 616 | "Epoch 5 Batch 100 Loss 0.057326 Acc 0.976562\n", 617 | "Epoch 5 Batch 200 Loss 0.037495 Acc 0.984375\n", 618 | "Epoch 5 Batch 300 Loss 0.025395 Acc 0.992188\n" 619 | ] 620 | } 621 | ], 622 | "source": [ 623 | "# 训练整个网络\n", 624 | "main = fluid.Program()\n", 625 | "startup = fluid.Program()\n", 626 | "exe = fluid.Executor(place)\n", 627 | "\n", 628 | "with fluid.unique_name.guard():\n", 629 | " with fluid.program_guard(main, startup):\n", 630 | "\n", 631 | " # 预测\n", 632 | " image = fluid.layers.data('image', [3, 32, 32], dtype='float32')\n", 633 | " predict, base_model_program = network(image, True)\n", 634 | " \n", 635 | " # 损失\n", 636 | " label = fluid.layers.data('label', [1], dtype='int64')\n", 637 | " loss = fluid.layers.cross_entropy(label=label, input=predict)\n", 638 | " loss = fluid.layers.mean(loss)\n", 639 | " \n", 640 | " # Metric\n", 641 | " acc = fluid.layers.accuracy(predict, label)\n", 642 | " \n", 643 | " test_program = main.clone(for_test=True)\n", 644 | " \n", 645 | " # Feeder\n", 646 | " feeder = fluid.data_feeder.DataFeeder([image, label], place)\n", 647 | " reader = feeder.decorate_reader(paddle.batch(paddle.dataset.cifar.train10(), 128), None)\n", 648 | " \n", 649 | " test_feeder = fluid.data_feeder.DataFeeder([image, label], place)\n", 650 | " test_reader = test_feeder.decorate_reader(paddle.batch(paddle.dataset.cifar.test10(), 128), None) \n", 651 | "\n", 652 | " # opt\n", 653 | " optimizer = fluid.optimizer.Adam(learning_rate=0.0001)\n", 654 | " optimizer.minimize(loss)\n", 655 | "\n", 656 | " # 初始化变量\n", 657 | " exe.run(startup)\n", 658 | "\n", 659 | " # 加载参数\n", 660 | " fluid.io.load_params(exe, \"cifar10.model\", main_program=main)\n", 661 | "\n", 662 | " optimized_main = fluid.transpiler.memory_optimize(main, print_log=False)\n", 663 | "\n", 664 | " \n", 665 | " for epoch in range(10):\n", 666 | " # 训练\n", 667 | " for batch, data in enumerate(reader()):\n", 668 | " result = exe.run(fetch_list=[loss, acc], feed=data)\n", 669 | " if batch % 100 == 0:\n", 670 | " print(\"Epoch %d Batch %d Loss %f Acc %f\" % (epoch, batch, result[0][0], result[1][0]))\n", 671 | " fluid.io.save_params(exe, \"cifar10.model\", main_program=main)\n", 672 | " \n", 673 | " # 测试\n", 674 | " test_acc = []\n", 675 | " for test_data in test_reader():\n", 676 | " result = exe.run(test_program, fetch_list=[acc], feed=test_data)\n", 677 | " test_acc.append(result[0][0])\n", 678 | " \n", 679 | " print(\"Epoch %d Test Acc %f\" % (epoch, np.mean(test_acc)))" 680 | ] 681 | }, 682 | { 683 | "cell_type": "code", 684 | "execution_count": null, 685 | "metadata": {}, 686 | "outputs": [], 687 | "source": [] 688 | } 689 | ], 690 | "metadata": { 691 | "kernelspec": { 692 | "display_name": "Python 2", 693 | "language": "python", 694 | "name": "python2" 695 | }, 696 | "language_info": { 697 | "codemirror_mode": { 698 | "name": "ipython", 699 | "version": 2 700 | }, 701 | "file_extension": ".py", 702 | "mimetype": "text/x-python", 703 | "name": "python", 704 | "nbconvert_exporter": "python", 705 | "pygments_lexer": "ipython2", 706 | "version": "2.7.5" 707 | } 708 | }, 709 | "nbformat": 4, 710 | "nbformat_minor": 2 711 | } 712 | -------------------------------------------------------------------------------- /paddle/startup_program.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": { 7 | "ExecuteTime": { 8 | "end_time": "2018-07-18T10:07:38.515437Z", 9 | "start_time": "2018-07-18T10:07:37.752668Z" 10 | } 11 | }, 12 | "outputs": [], 13 | "source": [ 14 | "import paddle.fluid as fluid\n", 15 | "import paddle\n", 16 | "import numpy as np\n", 17 | "from paddle.fluid.debugger import draw_block_graphviz" 18 | ] 19 | }, 20 | { 21 | "cell_type": "code", 22 | "execution_count": 2, 23 | "metadata": { 24 | "ExecuteTime": { 25 | "end_time": "2018-07-18T10:07:38.554849Z", 26 | "start_time": "2018-07-18T10:07:38.519481Z" 27 | } 28 | }, 29 | "outputs": [ 30 | { 31 | "name": "stderr", 32 | "output_type": "stream", 33 | "text": [ 34 | "WARNING:root:write block debug graph to predict.pdf\n", 35 | "WARNING:root:write block debug graph to opt.pdf\n" 36 | ] 37 | } 38 | ], 39 | "source": [ 40 | "new_startup = fluid.Program()\n", 41 | "new_main = fluid.Program()\n", 42 | "\n", 43 | "with fluid.program_guard(new_main, new_startup):\n", 44 | " image = fluid.layers.data(name='image', shape=[3, 24, 24], dtype='float32')\n", 45 | " predict = fluid.layers.fc(name='predict', input=image, size=1)\n", 46 | " draw_block_graphviz(new_main.blocks[0], path=\"predict.pdf\")\n", 47 | " optimizer = fluid.optimizer.Adam(learning_rate=0.001)\n", 48 | " optimizer.minimize(predict)\n", 49 | " draw_block_graphviz(new_main.blocks[0], path=\"opt.pdf\")" 50 | ] 51 | }, 52 | { 53 | "cell_type": "code", 54 | "execution_count": 3, 55 | "metadata": { 56 | "ExecuteTime": { 57 | "end_time": "2018-07-18T10:07:38.708935Z", 58 | "start_time": "2018-07-18T10:07:38.558986Z" 59 | }, 60 | "scrolled": true 61 | }, 62 | "outputs": [ 63 | { 64 | "data": { 65 | "text/plain": [ 66 | "blocks {\n", 67 | " idx: 0\n", 68 | " parent_idx: -1\n", 69 | " vars {\n", 70 | " name: \"learning_rate_0\"\n", 71 | " type {\n", 72 | " type: LOD_TENSOR\n", 73 | " lod_tensor {\n", 74 | " tensor {\n", 75 | " data_type: FP32\n", 76 | " dims: 1\n", 77 | " }\n", 78 | " }\n", 79 | " }\n", 80 | " persistable: true\n", 81 | " }\n", 82 | " vars {\n", 83 | " name: \"moment2_1\"\n", 84 | " type {\n", 85 | " type: LOD_TENSOR\n", 86 | " lod_tensor {\n", 87 | " tensor {\n", 88 | " data_type: FP32\n", 89 | " dims: 1728\n", 90 | " dims: 1\n", 91 | " }\n", 92 | " }\n", 93 | " }\n", 94 | " persistable: true\n", 95 | " }\n", 96 | " vars {\n", 97 | " name: \"moment1_0\"\n", 98 | " type {\n", 99 | " type: LOD_TENSOR\n", 100 | " lod_tensor {\n", 101 | " tensor {\n", 102 | " data_type: FP32\n", 103 | " dims: 1\n", 104 | " }\n", 105 | " }\n", 106 | " }\n", 107 | " persistable: true\n", 108 | " }\n", 109 | " vars {\n", 110 | " name: \"moment2_0\"\n", 111 | " type {\n", 112 | " type: LOD_TENSOR\n", 113 | " lod_tensor {\n", 114 | " tensor {\n", 115 | " data_type: FP32\n", 116 | " dims: 1\n", 117 | " }\n", 118 | " }\n", 119 | " }\n", 120 | " persistable: true\n", 121 | " }\n", 122 | " vars {\n", 123 | " name: \"beta2_pow_acc_0\"\n", 124 | " type {\n", 125 | " type: LOD_TENSOR\n", 126 | " lod_tensor {\n", 127 | " tensor {\n", 128 | " data_type: FP32\n", 129 | " dims: 1\n", 130 | " }\n", 131 | " }\n", 132 | " }\n", 133 | " persistable: true\n", 134 | " }\n", 135 | " vars {\n", 136 | " name: \"beta1_pow_acc_0\"\n", 137 | " type {\n", 138 | " type: LOD_TENSOR\n", 139 | " lod_tensor {\n", 140 | " tensor {\n", 141 | " data_type: FP32\n", 142 | " dims: 1\n", 143 | " }\n", 144 | " }\n", 145 | " }\n", 146 | " persistable: true\n", 147 | " }\n", 148 | " vars {\n", 149 | " name: \"moment1_1\"\n", 150 | " type {\n", 151 | " type: LOD_TENSOR\n", 152 | " lod_tensor {\n", 153 | " tensor {\n", 154 | " data_type: FP32\n", 155 | " dims: 1728\n", 156 | " dims: 1\n", 157 | " }\n", 158 | " }\n", 159 | " }\n", 160 | " persistable: true\n", 161 | " }\n", 162 | " vars {\n", 163 | " name: \"predict.b_0\"\n", 164 | " type {\n", 165 | " type: LOD_TENSOR\n", 166 | " lod_tensor {\n", 167 | " tensor {\n", 168 | " data_type: FP32\n", 169 | " dims: 1\n", 170 | " }\n", 171 | " }\n", 172 | " }\n", 173 | " persistable: true\n", 174 | " }\n", 175 | " vars {\n", 176 | " name: \"predict.w_0\"\n", 177 | " type {\n", 178 | " type: LOD_TENSOR\n", 179 | " lod_tensor {\n", 180 | " tensor {\n", 181 | " data_type: FP32\n", 182 | " dims: 1728\n", 183 | " dims: 1\n", 184 | " }\n", 185 | " }\n", 186 | " }\n", 187 | " persistable: true\n", 188 | " }\n", 189 | " ops {\n", 190 | " outputs {\n", 191 | " parameter: \"Out\"\n", 192 | " arguments: \"learning_rate_0\"\n", 193 | " }\n", 194 | " type: \"fill_constant\"\n", 195 | " attrs {\n", 196 | " name: \"op_role_var\"\n", 197 | " type: STRINGS\n", 198 | " }\n", 199 | " attrs {\n", 200 | " name: \"op_role\"\n", 201 | " type: INT\n", 202 | " i: 0\n", 203 | " }\n", 204 | " attrs {\n", 205 | " name: \"force_cpu\"\n", 206 | " type: BOOLEAN\n", 207 | " b: false\n", 208 | " }\n", 209 | " attrs {\n", 210 | " name: \"value\"\n", 211 | " type: FLOAT\n", 212 | " f: 0.0010000000475\n", 213 | " }\n", 214 | " attrs {\n", 215 | " name: \"shape\"\n", 216 | " type: INTS\n", 217 | " ints: 1\n", 218 | " }\n", 219 | " attrs {\n", 220 | " name: \"dtype\"\n", 221 | " type: INT\n", 222 | " i: 5\n", 223 | " }\n", 224 | " }\n", 225 | " ops {\n", 226 | " outputs {\n", 227 | " parameter: \"Out\"\n", 228 | " arguments: \"moment2_1\"\n", 229 | " }\n", 230 | " type: \"fill_constant\"\n", 231 | " attrs {\n", 232 | " name: \"op_role_var\"\n", 233 | " type: STRINGS\n", 234 | " }\n", 235 | " attrs {\n", 236 | " name: \"op_role\"\n", 237 | " type: INT\n", 238 | " i: 0\n", 239 | " }\n", 240 | " attrs {\n", 241 | " name: \"force_cpu\"\n", 242 | " type: BOOLEAN\n", 243 | " b: false\n", 244 | " }\n", 245 | " attrs {\n", 246 | " name: \"value\"\n", 247 | " type: FLOAT\n", 248 | " f: 0.0\n", 249 | " }\n", 250 | " attrs {\n", 251 | " name: \"shape\"\n", 252 | " type: INTS\n", 253 | " ints: 1728\n", 254 | " ints: 1\n", 255 | " }\n", 256 | " attrs {\n", 257 | " name: \"dtype\"\n", 258 | " type: INT\n", 259 | " i: 5\n", 260 | " }\n", 261 | " }\n", 262 | " ops {\n", 263 | " outputs {\n", 264 | " parameter: \"Out\"\n", 265 | " arguments: \"moment1_1\"\n", 266 | " }\n", 267 | " type: \"fill_constant\"\n", 268 | " attrs {\n", 269 | " name: \"op_role_var\"\n", 270 | " type: STRINGS\n", 271 | " }\n", 272 | " attrs {\n", 273 | " name: \"op_role\"\n", 274 | " type: INT\n", 275 | " i: 0\n", 276 | " }\n", 277 | " attrs {\n", 278 | " name: \"force_cpu\"\n", 279 | " type: BOOLEAN\n", 280 | " b: false\n", 281 | " }\n", 282 | " attrs {\n", 283 | " name: \"value\"\n", 284 | " type: FLOAT\n", 285 | " f: 0.0\n", 286 | " }\n", 287 | " attrs {\n", 288 | " name: \"shape\"\n", 289 | " type: INTS\n", 290 | " ints: 1728\n", 291 | " ints: 1\n", 292 | " }\n", 293 | " attrs {\n", 294 | " name: \"dtype\"\n", 295 | " type: INT\n", 296 | " i: 5\n", 297 | " }\n", 298 | " }\n", 299 | " ops {\n", 300 | " outputs {\n", 301 | " parameter: \"Out\"\n", 302 | " arguments: \"moment2_0\"\n", 303 | " }\n", 304 | " type: \"fill_constant\"\n", 305 | " attrs {\n", 306 | " name: \"op_role_var\"\n", 307 | " type: STRINGS\n", 308 | " }\n", 309 | " attrs {\n", 310 | " name: \"op_role\"\n", 311 | " type: INT\n", 312 | " i: 0\n", 313 | " }\n", 314 | " attrs {\n", 315 | " name: \"force_cpu\"\n", 316 | " type: BOOLEAN\n", 317 | " b: false\n", 318 | " }\n", 319 | " attrs {\n", 320 | " name: \"value\"\n", 321 | " type: FLOAT\n", 322 | " f: 0.0\n", 323 | " }\n", 324 | " attrs {\n", 325 | " name: \"shape\"\n", 326 | " type: INTS\n", 327 | " ints: 1\n", 328 | " }\n", 329 | " attrs {\n", 330 | " name: \"dtype\"\n", 331 | " type: INT\n", 332 | " i: 5\n", 333 | " }\n", 334 | " }\n", 335 | " ops {\n", 336 | " outputs {\n", 337 | " parameter: \"Out\"\n", 338 | " arguments: \"moment1_0\"\n", 339 | " }\n", 340 | " type: \"fill_constant\"\n", 341 | " attrs {\n", 342 | " name: \"op_role_var\"\n", 343 | " type: STRINGS\n", 344 | " }\n", 345 | " attrs {\n", 346 | " name: \"op_role\"\n", 347 | " type: INT\n", 348 | " i: 0\n", 349 | " }\n", 350 | " attrs {\n", 351 | " name: \"force_cpu\"\n", 352 | " type: BOOLEAN\n", 353 | " b: false\n", 354 | " }\n", 355 | " attrs {\n", 356 | " name: \"value\"\n", 357 | " type: FLOAT\n", 358 | " f: 0.0\n", 359 | " }\n", 360 | " attrs {\n", 361 | " name: \"shape\"\n", 362 | " type: INTS\n", 363 | " ints: 1\n", 364 | " }\n", 365 | " attrs {\n", 366 | " name: \"dtype\"\n", 367 | " type: INT\n", 368 | " i: 5\n", 369 | " }\n", 370 | " }\n", 371 | " ops {\n", 372 | " outputs {\n", 373 | " parameter: \"Out\"\n", 374 | " arguments: \"beta2_pow_acc_0\"\n", 375 | " }\n", 376 | " type: \"fill_constant\"\n", 377 | " attrs {\n", 378 | " name: \"op_role_var\"\n", 379 | " type: STRINGS\n", 380 | " }\n", 381 | " attrs {\n", 382 | " name: \"op_role\"\n", 383 | " type: INT\n", 384 | " i: 0\n", 385 | " }\n", 386 | " attrs {\n", 387 | " name: \"force_cpu\"\n", 388 | " type: BOOLEAN\n", 389 | " b: false\n", 390 | " }\n", 391 | " attrs {\n", 392 | " name: \"value\"\n", 393 | " type: FLOAT\n", 394 | " f: 0.999000012875\n", 395 | " }\n", 396 | " attrs {\n", 397 | " name: \"shape\"\n", 398 | " type: INTS\n", 399 | " ints: 1\n", 400 | " }\n", 401 | " attrs {\n", 402 | " name: \"dtype\"\n", 403 | " type: INT\n", 404 | " i: 5\n", 405 | " }\n", 406 | " }\n", 407 | " ops {\n", 408 | " outputs {\n", 409 | " parameter: \"Out\"\n", 410 | " arguments: \"beta1_pow_acc_0\"\n", 411 | " }\n", 412 | " type: \"fill_constant\"\n", 413 | " attrs {\n", 414 | " name: \"op_role_var\"\n", 415 | " type: STRINGS\n", 416 | " }\n", 417 | " attrs {\n", 418 | " name: \"op_role\"\n", 419 | " type: INT\n", 420 | " i: 0\n", 421 | " }\n", 422 | " attrs {\n", 423 | " name: \"force_cpu\"\n", 424 | " type: BOOLEAN\n", 425 | " b: false\n", 426 | " }\n", 427 | " attrs {\n", 428 | " name: \"value\"\n", 429 | " type: FLOAT\n", 430 | " f: 0.899999976158\n", 431 | " }\n", 432 | " attrs {\n", 433 | " name: \"shape\"\n", 434 | " type: INTS\n", 435 | " ints: 1\n", 436 | " }\n", 437 | " attrs {\n", 438 | " name: \"dtype\"\n", 439 | " type: INT\n", 440 | " i: 5\n", 441 | " }\n", 442 | " }\n", 443 | " ops {\n", 444 | " outputs {\n", 445 | " parameter: \"Out\"\n", 446 | " arguments: \"predict.b_0\"\n", 447 | " }\n", 448 | " type: \"fill_constant\"\n", 449 | " attrs {\n", 450 | " name: \"op_role_var\"\n", 451 | " type: STRINGS\n", 452 | " }\n", 453 | " attrs {\n", 454 | " name: \"op_role\"\n", 455 | " type: INT\n", 456 | " i: 0\n", 457 | " }\n", 458 | " attrs {\n", 459 | " name: \"force_cpu\"\n", 460 | " type: BOOLEAN\n", 461 | " b: false\n", 462 | " }\n", 463 | " attrs {\n", 464 | " name: \"value\"\n", 465 | " type: FLOAT\n", 466 | " f: 0.0\n", 467 | " }\n", 468 | " attrs {\n", 469 | " name: \"shape\"\n", 470 | " type: INTS\n", 471 | " ints: 1\n", 472 | " }\n", 473 | " attrs {\n", 474 | " name: \"dtype\"\n", 475 | " type: INT\n", 476 | " i: 5\n", 477 | " }\n", 478 | " }\n", 479 | " ops {\n", 480 | " outputs {\n", 481 | " parameter: \"Out\"\n", 482 | " arguments: \"predict.w_0\"\n", 483 | " }\n", 484 | " type: \"uniform_random\"\n", 485 | " attrs {\n", 486 | " name: \"op_role_var\"\n", 487 | " type: STRINGS\n", 488 | " }\n", 489 | " attrs {\n", 490 | " name: \"op_role\"\n", 491 | " type: INT\n", 492 | " i: 0\n", 493 | " }\n", 494 | " attrs {\n", 495 | " name: \"dtype\"\n", 496 | " type: INT\n", 497 | " i: 5\n", 498 | " }\n", 499 | " attrs {\n", 500 | " name: \"seed\"\n", 501 | " type: INT\n", 502 | " i: 0\n", 503 | " }\n", 504 | " attrs {\n", 505 | " name: \"min\"\n", 506 | " type: FLOAT\n", 507 | " f: -0.0589085221291\n", 508 | " }\n", 509 | " attrs {\n", 510 | " name: \"max\"\n", 511 | " type: FLOAT\n", 512 | " f: 0.0589085221291\n", 513 | " }\n", 514 | " attrs {\n", 515 | " name: \"shape\"\n", 516 | " type: INTS\n", 517 | " ints: 1728\n", 518 | " ints: 1\n", 519 | " }\n", 520 | " }\n", 521 | "}" 522 | ] 523 | }, 524 | "execution_count": 3, 525 | "metadata": {}, 526 | "output_type": "execute_result" 527 | } 528 | ], 529 | "source": [ 530 | "new_startup" 531 | ] 532 | }, 533 | { 534 | "cell_type": "code", 535 | "execution_count": 4, 536 | "metadata": { 537 | "ExecuteTime": { 538 | "end_time": "2018-07-18T10:07:38.808882Z", 539 | "start_time": "2018-07-18T10:07:38.712792Z" 540 | }, 541 | "scrolled": true 542 | }, 543 | "outputs": [ 544 | { 545 | "data": { 546 | "text/plain": [ 547 | "blocks {\n", 548 | " idx: 0\n", 549 | " parent_idx: -1\n", 550 | " vars {\n", 551 | " name: \"moment1_1\"\n", 552 | " type {\n", 553 | " type: LOD_TENSOR\n", 554 | " lod_tensor {\n", 555 | " tensor {\n", 556 | " data_type: FP32\n", 557 | " dims: 1728\n", 558 | " dims: 1\n", 559 | " }\n", 560 | " }\n", 561 | " }\n", 562 | " persistable: true\n", 563 | " }\n", 564 | " vars {\n", 565 | " name: \"moment2_0\"\n", 566 | " type {\n", 567 | " type: LOD_TENSOR\n", 568 | " lod_tensor {\n", 569 | " tensor {\n", 570 | " data_type: FP32\n", 571 | " dims: 1\n", 572 | " }\n", 573 | " }\n", 574 | " }\n", 575 | " persistable: true\n", 576 | " }\n", 577 | " vars {\n", 578 | " name: \"moment1_0\"\n", 579 | " type {\n", 580 | " type: LOD_TENSOR\n", 581 | " lod_tensor {\n", 582 | " tensor {\n", 583 | " data_type: FP32\n", 584 | " dims: 1\n", 585 | " }\n", 586 | " }\n", 587 | " }\n", 588 | " persistable: true\n", 589 | " }\n", 590 | " vars {\n", 591 | " name: \"beta2_pow_acc_0\"\n", 592 | " type {\n", 593 | " type: LOD_TENSOR\n", 594 | " lod_tensor {\n", 595 | " tensor {\n", 596 | " data_type: FP32\n", 597 | " dims: 1\n", 598 | " }\n", 599 | " lod_level: 0\n", 600 | " }\n", 601 | " }\n", 602 | " persistable: true\n", 603 | " }\n", 604 | " vars {\n", 605 | " name: \"image\"\n", 606 | " type {\n", 607 | " type: LOD_TENSOR\n", 608 | " lod_tensor {\n", 609 | " tensor {\n", 610 | " data_type: FP32\n", 611 | " dims: -1\n", 612 | " dims: 3\n", 613 | " dims: 24\n", 614 | " dims: 24\n", 615 | " }\n", 616 | " lod_level: 0\n", 617 | " }\n", 618 | " }\n", 619 | " persistable: false\n", 620 | " }\n", 621 | " vars {\n", 622 | " name: \"predict.w_0@GRAD\"\n", 623 | " type {\n", 624 | " type: LOD_TENSOR\n", 625 | " lod_tensor {\n", 626 | " tensor {\n", 627 | " data_type: FP32\n", 628 | " dims: 1728\n", 629 | " dims: 1\n", 630 | " }\n", 631 | " }\n", 632 | " }\n", 633 | " }\n", 634 | " vars {\n", 635 | " name: \"predict.w_0\"\n", 636 | " type {\n", 637 | " type: LOD_TENSOR\n", 638 | " lod_tensor {\n", 639 | " tensor {\n", 640 | " data_type: FP32\n", 641 | " dims: 1728\n", 642 | " dims: 1\n", 643 | " }\n", 644 | " }\n", 645 | " }\n", 646 | " persistable: true\n", 647 | " }\n", 648 | " vars {\n", 649 | " name: \"predict.tmp_0\"\n", 650 | " type {\n", 651 | " type: LOD_TENSOR\n", 652 | " lod_tensor {\n", 653 | " tensor {\n", 654 | " data_type: FP32\n", 655 | " dims: -1\n", 656 | " dims: 1\n", 657 | " }\n", 658 | " lod_level: 0\n", 659 | " }\n", 660 | " }\n", 661 | " persistable: false\n", 662 | " }\n", 663 | " vars {\n", 664 | " name: \"predict.b_0@GRAD\"\n", 665 | " type {\n", 666 | " type: LOD_TENSOR\n", 667 | " lod_tensor {\n", 668 | " tensor {\n", 669 | " data_type: FP32\n", 670 | " dims: 1\n", 671 | " }\n", 672 | " }\n", 673 | " }\n", 674 | " }\n", 675 | " vars {\n", 676 | " name: \"predict.b_0\"\n", 677 | " type {\n", 678 | " type: LOD_TENSOR\n", 679 | " lod_tensor {\n", 680 | " tensor {\n", 681 | " data_type: FP32\n", 682 | " dims: 1\n", 683 | " }\n", 684 | " }\n", 685 | " }\n", 686 | " persistable: true\n", 687 | " }\n", 688 | " vars {\n", 689 | " name: \"predict.tmp_1\"\n", 690 | " type {\n", 691 | " type: LOD_TENSOR\n", 692 | " lod_tensor {\n", 693 | " tensor {\n", 694 | " data_type: FP32\n", 695 | " dims: -1\n", 696 | " dims: 1\n", 697 | " }\n", 698 | " lod_level: 0\n", 699 | " }\n", 700 | " }\n", 701 | " persistable: false\n", 702 | " }\n", 703 | " vars {\n", 704 | " name: \"predict.tmp_1@GRAD\"\n", 705 | " type {\n", 706 | " type: LOD_TENSOR\n", 707 | " lod_tensor {\n", 708 | " tensor {\n", 709 | " data_type: FP32\n", 710 | " dims: 1\n", 711 | " }\n", 712 | " }\n", 713 | " }\n", 714 | " persistable: true\n", 715 | " }\n", 716 | " vars {\n", 717 | " name: \"predict.tmp_0@GRAD\"\n", 718 | " type {\n", 719 | " type: LOD_TENSOR\n", 720 | " lod_tensor {\n", 721 | " tensor {\n", 722 | " data_type: FP32\n", 723 | " dims: -1\n", 724 | " dims: 1\n", 725 | " }\n", 726 | " }\n", 727 | " }\n", 728 | " }\n", 729 | " vars {\n", 730 | " name: \"learning_rate_0\"\n", 731 | " type {\n", 732 | " type: LOD_TENSOR\n", 733 | " lod_tensor {\n", 734 | " tensor {\n", 735 | " data_type: FP32\n", 736 | " dims: 1\n", 737 | " }\n", 738 | " }\n", 739 | " }\n", 740 | " persistable: true\n", 741 | " }\n", 742 | " vars {\n", 743 | " name: \"moment2_1\"\n", 744 | " type {\n", 745 | " type: LOD_TENSOR\n", 746 | " lod_tensor {\n", 747 | " tensor {\n", 748 | " data_type: FP32\n", 749 | " dims: 1728\n", 750 | " dims: 1\n", 751 | " }\n", 752 | " }\n", 753 | " }\n", 754 | " persistable: true\n", 755 | " }\n", 756 | " vars {\n", 757 | " name: \"beta1_pow_acc_0\"\n", 758 | " type {\n", 759 | " type: LOD_TENSOR\n", 760 | " lod_tensor {\n", 761 | " tensor {\n", 762 | " data_type: FP32\n", 763 | " dims: 1\n", 764 | " }\n", 765 | " lod_level: 0\n", 766 | " }\n", 767 | " }\n", 768 | " persistable: true\n", 769 | " }\n", 770 | " ops {\n", 771 | " inputs {\n", 772 | " parameter: \"X\"\n", 773 | " arguments: \"image\"\n", 774 | " }\n", 775 | " inputs {\n", 776 | " parameter: \"Y\"\n", 777 | " arguments: \"predict.w_0\"\n", 778 | " }\n", 779 | " outputs {\n", 780 | " parameter: \"Out\"\n", 781 | " arguments: \"predict.tmp_0\"\n", 782 | " }\n", 783 | " type: \"mul\"\n", 784 | " attrs {\n", 785 | " name: \"op_role_var\"\n", 786 | " type: STRINGS\n", 787 | " }\n", 788 | " attrs {\n", 789 | " name: \"op_role\"\n", 790 | " type: INT\n", 791 | " i: 0\n", 792 | " }\n", 793 | " attrs {\n", 794 | " name: \"y_num_col_dims\"\n", 795 | " type: INT\n", 796 | " i: 1\n", 797 | " }\n", 798 | " attrs {\n", 799 | " name: \"x_num_col_dims\"\n", 800 | " type: INT\n", 801 | " i: 1\n", 802 | " }\n", 803 | " }\n", 804 | " ops {\n", 805 | " inputs {\n", 806 | " parameter: \"X\"\n", 807 | " arguments: \"predict.tmp_0\"\n", 808 | " }\n", 809 | " inputs {\n", 810 | " parameter: \"Y\"\n", 811 | " arguments: \"predict.b_0\"\n", 812 | " }\n", 813 | " outputs {\n", 814 | " parameter: \"Out\"\n", 815 | " arguments: \"predict.tmp_1\"\n", 816 | " }\n", 817 | " type: \"elementwise_add\"\n", 818 | " attrs {\n", 819 | " name: \"op_role_var\"\n", 820 | " type: STRINGS\n", 821 | " }\n", 822 | " attrs {\n", 823 | " name: \"op_role\"\n", 824 | " type: INT\n", 825 | " i: 256\n", 826 | " }\n", 827 | " attrs {\n", 828 | " name: \"axis\"\n", 829 | " type: INT\n", 830 | " i: 1\n", 831 | " }\n", 832 | " }\n", 833 | " ops {\n", 834 | " outputs {\n", 835 | " parameter: \"Out\"\n", 836 | " arguments: \"predict.tmp_1@GRAD\"\n", 837 | " }\n", 838 | " type: \"fill_constant\"\n", 839 | " attrs {\n", 840 | " name: \"op_role\"\n", 841 | " type: INT\n", 842 | " i: 257\n", 843 | " }\n", 844 | " attrs {\n", 845 | " name: \"value\"\n", 846 | " type: FLOAT\n", 847 | " f: 1.0\n", 848 | " }\n", 849 | " attrs {\n", 850 | " name: \"force_cpu\"\n", 851 | " type: INT\n", 852 | " i: 0\n", 853 | " }\n", 854 | " attrs {\n", 855 | " name: \"shape\"\n", 856 | " type: INTS\n", 857 | " ints: 1\n", 858 | " }\n", 859 | " attrs {\n", 860 | " name: \"dtype\"\n", 861 | " type: INT\n", 862 | " i: 5\n", 863 | " }\n", 864 | " }\n", 865 | " ops {\n", 866 | " inputs {\n", 867 | " parameter: \"Out\"\n", 868 | " arguments: \"predict.tmp_1\"\n", 869 | " }\n", 870 | " inputs {\n", 871 | " parameter: \"Out@GRAD\"\n", 872 | " arguments: \"predict.tmp_1@GRAD\"\n", 873 | " }\n", 874 | " inputs {\n", 875 | " parameter: \"X\"\n", 876 | " arguments: \"predict.tmp_0\"\n", 877 | " }\n", 878 | " inputs {\n", 879 | " parameter: \"Y\"\n", 880 | " arguments: \"predict.b_0\"\n", 881 | " }\n", 882 | " outputs {\n", 883 | " parameter: \"X@GRAD\"\n", 884 | " arguments: \"predict.tmp_0@GRAD\"\n", 885 | " }\n", 886 | " outputs {\n", 887 | " parameter: \"Y@GRAD\"\n", 888 | " arguments: \"predict.b_0@GRAD\"\n", 889 | " }\n", 890 | " type: \"elementwise_add_grad\"\n", 891 | " attrs {\n", 892 | " name: \"op_role_var\"\n", 893 | " type: STRINGS\n", 894 | " strings: \"predict.b_0\"\n", 895 | " strings: \"predict.b_0@GRAD\"\n", 896 | " }\n", 897 | " attrs {\n", 898 | " name: \"op_role\"\n", 899 | " type: INT\n", 900 | " i: 1\n", 901 | " }\n", 902 | " attrs {\n", 903 | " name: \"axis\"\n", 904 | " type: INT\n", 905 | " i: 1\n", 906 | " }\n", 907 | " }\n", 908 | " ops {\n", 909 | " inputs {\n", 910 | " parameter: \"Out\"\n", 911 | " arguments: \"predict.tmp_0\"\n", 912 | " }\n", 913 | " inputs {\n", 914 | " parameter: \"Out@GRAD\"\n", 915 | " arguments: \"predict.tmp_0@GRAD\"\n", 916 | " }\n", 917 | " inputs {\n", 918 | " parameter: \"X\"\n", 919 | " arguments: \"image\"\n", 920 | " }\n", 921 | " inputs {\n", 922 | " parameter: \"Y\"\n", 923 | " arguments: \"predict.w_0\"\n", 924 | " }\n", 925 | " outputs {\n", 926 | " parameter: \"X@GRAD\"\n", 927 | " }\n", 928 | " outputs {\n", 929 | " parameter: \"Y@GRAD\"\n", 930 | " arguments: \"predict.w_0@GRAD\"\n", 931 | " }\n", 932 | " type: \"mul_grad\"\n", 933 | " attrs {\n", 934 | " name: \"op_role_var\"\n", 935 | " type: STRINGS\n", 936 | " strings: \"predict.w_0\"\n", 937 | " strings: \"predict.w_0@GRAD\"\n", 938 | " }\n", 939 | " attrs {\n", 940 | " name: \"op_role\"\n", 941 | " type: INT\n", 942 | " i: 1\n", 943 | " }\n", 944 | " attrs {\n", 945 | " name: \"y_num_col_dims\"\n", 946 | " type: INT\n", 947 | " i: 1\n", 948 | " }\n", 949 | " attrs {\n", 950 | " name: \"x_num_col_dims\"\n", 951 | " type: INT\n", 952 | " i: 1\n", 953 | " }\n", 954 | " }\n", 955 | " ops {\n", 956 | " inputs {\n", 957 | " parameter: \"Beta1Pow\"\n", 958 | " arguments: \"beta1_pow_acc_0\"\n", 959 | " }\n", 960 | " inputs {\n", 961 | " parameter: \"Beta2Pow\"\n", 962 | " arguments: \"beta2_pow_acc_0\"\n", 963 | " }\n", 964 | " inputs {\n", 965 | " parameter: \"Grad\"\n", 966 | " arguments: \"predict.b_0@GRAD\"\n", 967 | " }\n", 968 | " inputs {\n", 969 | " parameter: \"LearningRate\"\n", 970 | " arguments: \"learning_rate_0\"\n", 971 | " }\n", 972 | " inputs {\n", 973 | " parameter: \"Moment1\"\n", 974 | " arguments: \"moment1_0\"\n", 975 | " }\n", 976 | " inputs {\n", 977 | " parameter: \"Moment2\"\n", 978 | " arguments: \"moment2_0\"\n", 979 | " }\n", 980 | " inputs {\n", 981 | " parameter: \"Param\"\n", 982 | " arguments: \"predict.b_0\"\n", 983 | " }\n", 984 | " outputs {\n", 985 | " parameter: \"Moment1Out\"\n", 986 | " arguments: \"moment1_0\"\n", 987 | " }\n", 988 | " outputs {\n", 989 | " parameter: \"Moment2Out\"\n", 990 | " arguments: \"moment2_0\"\n", 991 | " }\n", 992 | " outputs {\n", 993 | " parameter: \"ParamOut\"\n", 994 | " arguments: \"predict.b_0\"\n", 995 | " }\n", 996 | " type: \"adam\"\n", 997 | " attrs {\n", 998 | " name: \"op_role_var\"\n", 999 | " type: STRINGS\n", 1000 | " strings: \"predict.b_0\"\n", 1001 | " }\n", 1002 | " attrs {\n", 1003 | " name: \"op_role\"\n", 1004 | " type: INT\n", 1005 | " i: 2\n", 1006 | " }\n", 1007 | " attrs {\n", 1008 | " name: \"epsilon\"\n", 1009 | " type: FLOAT\n", 1010 | " f: 9.99999993923e-09\n", 1011 | " }\n", 1012 | " attrs {\n", 1013 | " name: \"beta2\"\n", 1014 | " type: FLOAT\n", 1015 | " f: 0.999000012875\n", 1016 | " }\n", 1017 | " attrs {\n", 1018 | " name: \"beta1\"\n", 1019 | " type: FLOAT\n", 1020 | " f: 0.899999976158\n", 1021 | " }\n", 1022 | " }\n", 1023 | " ops {\n", 1024 | " inputs {\n", 1025 | " parameter: \"Beta1Pow\"\n", 1026 | " arguments: \"beta1_pow_acc_0\"\n", 1027 | " }\n", 1028 | " inputs {\n", 1029 | " parameter: \"Beta2Pow\"\n", 1030 | " arguments: \"beta2_pow_acc_0\"\n", 1031 | " }\n", 1032 | " inputs {\n", 1033 | " parameter: \"Grad\"\n", 1034 | " arguments: \"predict.w_0@GRAD\"\n", 1035 | " }\n", 1036 | " inputs {\n", 1037 | " parameter: \"LearningRate\"\n", 1038 | " arguments: \"learning_rate_0\"\n", 1039 | " }\n", 1040 | " inputs {\n", 1041 | " parameter: \"Moment1\"\n", 1042 | " arguments: \"moment1_1\"\n", 1043 | " }\n", 1044 | " inputs {\n", 1045 | " parameter: \"Moment2\"\n", 1046 | " arguments: \"moment2_1\"\n", 1047 | " }\n", 1048 | " inputs {\n", 1049 | " parameter: \"Param\"\n", 1050 | " arguments: \"predict.w_0\"\n", 1051 | " }\n", 1052 | " outputs {\n", 1053 | " parameter: \"Moment1Out\"\n", 1054 | " arguments: \"moment1_1\"\n", 1055 | " }\n", 1056 | " outputs {\n", 1057 | " parameter: \"Moment2Out\"\n", 1058 | " arguments: \"moment2_1\"\n", 1059 | " }\n", 1060 | " outputs {\n", 1061 | " parameter: \"ParamOut\"\n", 1062 | " arguments: \"predict.w_0\"\n", 1063 | " }\n", 1064 | " type: \"adam\"\n", 1065 | " attrs {\n", 1066 | " name: \"op_role_var\"\n", 1067 | " type: STRINGS\n", 1068 | " strings: \"predict.w_0\"\n", 1069 | " }\n", 1070 | " attrs {\n", 1071 | " name: \"op_role\"\n", 1072 | " type: INT\n", 1073 | " i: 2\n", 1074 | " }\n", 1075 | " attrs {\n", 1076 | " name: \"epsilon\"\n", 1077 | " type: FLOAT\n", 1078 | " f: 9.99999993923e-09\n", 1079 | " }\n", 1080 | " attrs {\n", 1081 | " name: \"beta2\"\n", 1082 | " type: FLOAT\n", 1083 | " f: 0.999000012875\n", 1084 | " }\n", 1085 | " attrs {\n", 1086 | " name: \"beta1\"\n", 1087 | " type: FLOAT\n", 1088 | " f: 0.899999976158\n", 1089 | " }\n", 1090 | " }\n", 1091 | " ops {\n", 1092 | " inputs {\n", 1093 | " parameter: \"X\"\n", 1094 | " arguments: \"beta1_pow_acc_0\"\n", 1095 | " }\n", 1096 | " outputs {\n", 1097 | " parameter: \"Out\"\n", 1098 | " arguments: \"beta1_pow_acc_0\"\n", 1099 | " }\n", 1100 | " type: \"scale\"\n", 1101 | " attrs {\n", 1102 | " name: \"op_role_var\"\n", 1103 | " type: STRINGS\n", 1104 | " }\n", 1105 | " attrs {\n", 1106 | " name: \"op_role\"\n", 1107 | " type: INT\n", 1108 | " i: 0\n", 1109 | " }\n", 1110 | " attrs {\n", 1111 | " name: \"scale\"\n", 1112 | " type: FLOAT\n", 1113 | " f: 0.899999976158\n", 1114 | " }\n", 1115 | " }\n", 1116 | " ops {\n", 1117 | " inputs {\n", 1118 | " parameter: \"X\"\n", 1119 | " arguments: \"beta2_pow_acc_0\"\n", 1120 | " }\n", 1121 | " outputs {\n", 1122 | " parameter: \"Out\"\n", 1123 | " arguments: \"beta2_pow_acc_0\"\n", 1124 | " }\n", 1125 | " type: \"scale\"\n", 1126 | " attrs {\n", 1127 | " name: \"op_role_var\"\n", 1128 | " type: STRINGS\n", 1129 | " }\n", 1130 | " attrs {\n", 1131 | " name: \"op_role\"\n", 1132 | " type: INT\n", 1133 | " i: 0\n", 1134 | " }\n", 1135 | " attrs {\n", 1136 | " name: \"scale\"\n", 1137 | " type: FLOAT\n", 1138 | " f: 0.999000012875\n", 1139 | " }\n", 1140 | " }\n", 1141 | "}" 1142 | ] 1143 | }, 1144 | "execution_count": 4, 1145 | "metadata": {}, 1146 | "output_type": "execute_result" 1147 | } 1148 | ], 1149 | "source": [ 1150 | "new_main" 1151 | ] 1152 | }, 1153 | { 1154 | "cell_type": "code", 1155 | "execution_count": 5, 1156 | "metadata": { 1157 | "ExecuteTime": { 1158 | "end_time": "2018-07-18T10:07:38.863995Z", 1159 | "start_time": "2018-07-18T10:07:38.813235Z" 1160 | } 1161 | }, 1162 | "outputs": [], 1163 | "source": [ 1164 | "startup = fluid.default_startup_program()" 1165 | ] 1166 | }, 1167 | { 1168 | "cell_type": "code", 1169 | "execution_count": 6, 1170 | "metadata": { 1171 | "ExecuteTime": { 1172 | "end_time": "2018-07-18T10:07:38.938830Z", 1173 | "start_time": "2018-07-18T10:07:38.867649Z" 1174 | } 1175 | }, 1176 | "outputs": [ 1177 | { 1178 | "data": { 1179 | "text/plain": [ 1180 | "blocks {\n", 1181 | " idx: 0\n", 1182 | " parent_idx: -1\n", 1183 | "}" 1184 | ] 1185 | }, 1186 | "execution_count": 6, 1187 | "metadata": {}, 1188 | "output_type": "execute_result" 1189 | } 1190 | ], 1191 | "source": [ 1192 | "startup" 1193 | ] 1194 | }, 1195 | { 1196 | "cell_type": "code", 1197 | "execution_count": 7, 1198 | "metadata": { 1199 | "ExecuteTime": { 1200 | "end_time": "2018-07-18T10:07:39.006846Z", 1201 | "start_time": "2018-07-18T10:07:38.944850Z" 1202 | } 1203 | }, 1204 | "outputs": [], 1205 | "source": [ 1206 | "exe = fluid.executor.Executor(fluid.CPUPlace())" 1207 | ] 1208 | }, 1209 | { 1210 | "cell_type": "code", 1211 | "execution_count": 8, 1212 | "metadata": { 1213 | "ExecuteTime": { 1214 | "end_time": "2018-07-18T10:07:39.124882Z", 1215 | "start_time": "2018-07-18T10:07:39.011236Z" 1216 | } 1217 | }, 1218 | "outputs": [], 1219 | "source": [ 1220 | "feed={'image': np.random.random((3,3,24,24)).astype('float32')}" 1221 | ] 1222 | }, 1223 | { 1224 | "cell_type": "code", 1225 | "execution_count": 9, 1226 | "metadata": { 1227 | "ExecuteTime": { 1228 | "end_time": "2018-07-18T10:07:39.432596Z", 1229 | "start_time": "2018-07-18T10:07:39.139315Z" 1230 | } 1231 | }, 1232 | "outputs": [ 1233 | { 1234 | "ename": "EnforceNotMet", 1235 | "evalue": "enforce y_dims.size() > y_num_col_dims failed, 1 <= 1\nThe input tensor Y's rank of MulOp should be larger than y_num_col_dims. at [/paddle/paddle/fluid/operators/mul_op.cc:52]\nPaddlePaddle Call Stacks: \n0 0x7f3f08976376p paddle::platform::EnforceNotMet::EnforceNotMet(std::__exception_ptr::exception_ptr, char const*, int) + 486\n1 0x7f3f08c9dba6p paddle::operators::MulOp::InferShape(paddle::framework::InferShapeContext*) const + 2774\n2 0x7f3f091918ebp paddle::framework::OperatorWithKernel::RunImpl(paddle::framework::Scope const&, boost::variant const&) const + 91\n3 0x7f3f0918f450p paddle::framework::OperatorBase::Run(paddle::framework::Scope const&, boost::variant const&) + 208\n4 0x7f3f08a09cdfp paddle::framework::Executor::RunPreparedContext(paddle::framework::ExecutorPrepareContext*, paddle::framework::Scope*, bool, bool, bool) + 255\n5 0x7f3f08a0ad30p paddle::framework::Executor::Run(paddle::framework::ProgramDesc const&, paddle::framework::Scope*, int, bool, bool) + 128\n6 0x7f3f0898cfabp pybind11::cpp_function::initialize(void (paddle::framework::Executor::*)(paddle::framework::ProgramDesc const&, paddle::framework::Scope*, int, bool, bool), pybind11::name const&, pybind11::is_method const&, pybind11::sibling const&)::{lambda(paddle::framework::Executor*, paddle::framework::ProgramDesc const&, paddle::framework::Scope*, int, bool, bool)#1}, void, paddle::framework::Executor*, paddle::framework::ProgramDesc const&, paddle::framework::Scope*, int, bool, bool, pybind11::name, pybind11::is_method, pybind11::sibling>(pybind11::cpp_function::initialize(void (paddle::framework::Executor::*)(paddle::framework::ProgramDesc const&, paddle::framework::Scope*, int, bool, bool), pybind11::name const&, pybind11::is_method const&, pybind11::sibling const&)::{lambda(paddle::framework::Executor*, paddle::framework::ProgramDesc const&, paddle::framework::Scope*, int, bool, bool)#1}&&, void (*)(paddle::framework::Executor*, paddle::framework::ProgramDesc const&, paddle::framework::Scope*, int, bool, bool), pybind11::name const&, pybind11::is_method const&, pybind11::sibling const&)::{lambda(pybind11::detail::function_call&)#3}::_FUN(pybind11::detail::function_call) + 555\n7 0x7f3f0898546cp pybind11::cpp_function::dispatcher(_object*, _object*, _object*) + 2540\n8 0x7f3f526971a8p PyEval_EvalFrameEx + 25384\n9 0x7f3f526ec0dap PyEval_EvalCodeEx + 714\n10 0x7f3f5269697fp PyEval_EvalFrameEx + 23295\n11 0x7f3f526ec0dap PyEval_EvalCodeEx + 714\n12 0x7f3f5270b30ap PyEval_EvalCode + 26\n13 0x7f3f52695fcfp PyEval_EvalFrameEx + 20815\n14 0x7f3f526ec0dap PyEval_EvalCodeEx + 714\n15 0x7f3f5269697fp PyEval_EvalFrameEx + 23295\n16 0x7f3f526ec0dap PyEval_EvalCodeEx + 714\n17 0x7f3f5269697fp PyEval_EvalFrameEx + 23295\n18 0x7f3f526ec0dap PyEval_EvalCodeEx + 714\n19 0x7f3f5267dfb8p\n20 0x7f3f526350e3p PyObject_Call + 67\n21 0x7f3f52694259p PyEval_EvalFrameEx + 13273\n22 0x7f3f526ec0dap PyEval_EvalCodeEx + 714\n23 0x7f3f52696dbfp PyEval_EvalFrameEx + 24383\n24 0x7f3f526ec0dap PyEval_EvalCodeEx + 714\n25 0x7f3f5269697fp PyEval_EvalFrameEx + 23295\n26 0x7f3f526ec0dap PyEval_EvalCodeEx + 714\n27 0x7f3f52696dbfp PyEval_EvalFrameEx + 24383\n28 0x7f3f526ec0dap PyEval_EvalCodeEx + 714\n29 0x7f3f52696dbfp PyEval_EvalFrameEx + 24383\n30 0x7f3f526ec0dap PyEval_EvalCodeEx + 714\n31 0x7f3f5267dfb8p\n32 0x7f3f526350e3p PyObject_Call + 67\n33 0x7f3f52694259p PyEval_EvalFrameEx + 13273\n34 0x7f3f526ec0dap PyEval_EvalCodeEx + 714\n35 0x7f3f5267dfb8p\n36 0x7f3f526350e3p PyObject_Call + 67\n37 0x7f3f52694259p PyEval_EvalFrameEx + 13273\n38 0x7f3f526ec0dap PyEval_EvalCodeEx + 714\n39 0x7f3f52696dbfp PyEval_EvalFrameEx + 24383\n40 0x7f3f526ec0dap PyEval_EvalCodeEx + 714\n41 0x7f3f52696dbfp PyEval_EvalFrameEx + 24383\n42 0x7f3f526ec0dap PyEval_EvalCodeEx + 714\n43 0x7f3f52696dbfp PyEval_EvalFrameEx + 24383\n44 0x7f3f526ec0dap PyEval_EvalCodeEx + 714\n45 0x7f3f5267dfb8p\n46 0x7f3f526350e3p PyObject_Call + 67\n47 0x7f3f52694259p PyEval_EvalFrameEx + 13273\n48 0x7f3f526ec0dap PyEval_EvalCodeEx + 714\n49 0x7f3f5267de0fp\n50 0x7f3f526350e3p PyObject_Call + 67\n51 0x7f3f4f3e789fp\n52 0x7f3f526350e3p PyObject_Call + 67\n53 0x7f3f5269680ep PyEval_EvalFrameEx + 22926\n54 0x7f3f526ec0dap PyEval_EvalCodeEx + 714\n55 0x7f3f52696dbfp PyEval_EvalFrameEx + 24383\n56 0x7f3f526ec0dap PyEval_EvalCodeEx + 714\n57 0x7f3f52696dbfp PyEval_EvalFrameEx + 24383\n58 0x7f3f526ec0dap PyEval_EvalCodeEx + 714\n59 0x7f3f52696dbfp PyEval_EvalFrameEx + 24383\n60 0x7f3f526ec0dap PyEval_EvalCodeEx + 714\n61 0x7f3f5269697fp PyEval_EvalFrameEx + 23295\n62 0x7f3f526ec0dap PyEval_EvalCodeEx + 714\n63 0x7f3f5270b30ap PyEval_EvalCode + 26\n64 0x7f3f52695fcfp PyEval_EvalFrameEx + 20815\n65 0x7f3f526ec0dap PyEval_EvalCodeEx + 714\n66 0x7f3f5269697fp PyEval_EvalFrameEx + 23295\n67 0x7f3f526ec0dap PyEval_EvalCodeEx + 714\n68 0x7f3f5267de0fp\n69 0x7f3f526350e3p PyObject_Call + 67\n70 0x7f3f526f3a14p\n71 0x7f3f526f3ec0p Py_Main + 1088\n72 0x7f3f52a1106bp __libc_start_main + 235\n73 0x55b3f4e5077ap _start + 42\n", 1236 | "output_type": "error", 1237 | "traceback": [ 1238 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 1239 | "\u001b[0;31mEnforceNotMet\u001b[0m Traceback (most recent call last)", 1240 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mexe\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrun\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mprogram\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mnew_main\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfetch_list\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mpredict\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfeed\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mfeed\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# Not init\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 1241 | "\u001b[0;32m/home/lyp/projects/paddle/.env/lib/python2.7/site-packages/paddle/fluid/executor.pyc\u001b[0m in \u001b[0;36mrun\u001b[0;34m(self, program, feed, fetch_list, feed_var_name, fetch_var_name, scope, return_numpy, use_program_cache)\u001b[0m\n\u001b[1;32m 441\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 442\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_feed_data\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mprogram\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfeed\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfeed_var_name\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mscope\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 443\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mexecutor\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrun\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mprogram\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdesc\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mscope\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mTrue\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mTrue\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 444\u001b[0m \u001b[0mouts\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_fetch_data\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfetch_list\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfetch_var_name\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mscope\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 445\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mreturn_numpy\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 1242 | "\u001b[0;31mEnforceNotMet\u001b[0m: enforce y_dims.size() > y_num_col_dims failed, 1 <= 1\nThe input tensor Y's rank of MulOp should be larger than y_num_col_dims. at [/paddle/paddle/fluid/operators/mul_op.cc:52]\nPaddlePaddle Call Stacks: \n0 0x7f3f08976376p paddle::platform::EnforceNotMet::EnforceNotMet(std::__exception_ptr::exception_ptr, char const*, int) + 486\n1 0x7f3f08c9dba6p paddle::operators::MulOp::InferShape(paddle::framework::InferShapeContext*) const + 2774\n2 0x7f3f091918ebp paddle::framework::OperatorWithKernel::RunImpl(paddle::framework::Scope const&, boost::variant const&) const + 91\n3 0x7f3f0918f450p paddle::framework::OperatorBase::Run(paddle::framework::Scope const&, boost::variant const&) + 208\n4 0x7f3f08a09cdfp paddle::framework::Executor::RunPreparedContext(paddle::framework::ExecutorPrepareContext*, paddle::framework::Scope*, bool, bool, bool) + 255\n5 0x7f3f08a0ad30p paddle::framework::Executor::Run(paddle::framework::ProgramDesc const&, paddle::framework::Scope*, int, bool, bool) + 128\n6 0x7f3f0898cfabp pybind11::cpp_function::initialize(void (paddle::framework::Executor::*)(paddle::framework::ProgramDesc const&, paddle::framework::Scope*, int, bool, bool), pybind11::name const&, pybind11::is_method const&, pybind11::sibling const&)::{lambda(paddle::framework::Executor*, paddle::framework::ProgramDesc const&, paddle::framework::Scope*, int, bool, bool)#1}, void, paddle::framework::Executor*, paddle::framework::ProgramDesc const&, paddle::framework::Scope*, int, bool, bool, pybind11::name, pybind11::is_method, pybind11::sibling>(pybind11::cpp_function::initialize(void (paddle::framework::Executor::*)(paddle::framework::ProgramDesc const&, paddle::framework::Scope*, int, bool, bool), pybind11::name const&, pybind11::is_method const&, pybind11::sibling const&)::{lambda(paddle::framework::Executor*, paddle::framework::ProgramDesc const&, paddle::framework::Scope*, int, bool, bool)#1}&&, void (*)(paddle::framework::Executor*, paddle::framework::ProgramDesc const&, paddle::framework::Scope*, int, bool, bool), pybind11::name const&, pybind11::is_method const&, pybind11::sibling const&)::{lambda(pybind11::detail::function_call&)#3}::_FUN(pybind11::detail::function_call) + 555\n7 0x7f3f0898546cp pybind11::cpp_function::dispatcher(_object*, _object*, _object*) + 2540\n8 0x7f3f526971a8p PyEval_EvalFrameEx + 25384\n9 0x7f3f526ec0dap PyEval_EvalCodeEx + 714\n10 0x7f3f5269697fp PyEval_EvalFrameEx + 23295\n11 0x7f3f526ec0dap PyEval_EvalCodeEx + 714\n12 0x7f3f5270b30ap PyEval_EvalCode + 26\n13 0x7f3f52695fcfp PyEval_EvalFrameEx + 20815\n14 0x7f3f526ec0dap PyEval_EvalCodeEx + 714\n15 0x7f3f5269697fp PyEval_EvalFrameEx + 23295\n16 0x7f3f526ec0dap PyEval_EvalCodeEx + 714\n17 0x7f3f5269697fp PyEval_EvalFrameEx + 23295\n18 0x7f3f526ec0dap PyEval_EvalCodeEx + 714\n19 0x7f3f5267dfb8p\n20 0x7f3f526350e3p PyObject_Call + 67\n21 0x7f3f52694259p PyEval_EvalFrameEx + 13273\n22 0x7f3f526ec0dap PyEval_EvalCodeEx + 714\n23 0x7f3f52696dbfp PyEval_EvalFrameEx + 24383\n24 0x7f3f526ec0dap PyEval_EvalCodeEx + 714\n25 0x7f3f5269697fp PyEval_EvalFrameEx + 23295\n26 0x7f3f526ec0dap PyEval_EvalCodeEx + 714\n27 0x7f3f52696dbfp PyEval_EvalFrameEx + 24383\n28 0x7f3f526ec0dap PyEval_EvalCodeEx + 714\n29 0x7f3f52696dbfp PyEval_EvalFrameEx + 24383\n30 0x7f3f526ec0dap PyEval_EvalCodeEx + 714\n31 0x7f3f5267dfb8p\n32 0x7f3f526350e3p PyObject_Call + 67\n33 0x7f3f52694259p PyEval_EvalFrameEx + 13273\n34 0x7f3f526ec0dap PyEval_EvalCodeEx + 714\n35 0x7f3f5267dfb8p\n36 0x7f3f526350e3p PyObject_Call + 67\n37 0x7f3f52694259p PyEval_EvalFrameEx + 13273\n38 0x7f3f526ec0dap PyEval_EvalCodeEx + 714\n39 0x7f3f52696dbfp PyEval_EvalFrameEx + 24383\n40 0x7f3f526ec0dap PyEval_EvalCodeEx + 714\n41 0x7f3f52696dbfp PyEval_EvalFrameEx + 24383\n42 0x7f3f526ec0dap PyEval_EvalCodeEx + 714\n43 0x7f3f52696dbfp PyEval_EvalFrameEx + 24383\n44 0x7f3f526ec0dap PyEval_EvalCodeEx + 714\n45 0x7f3f5267dfb8p\n46 0x7f3f526350e3p PyObject_Call + 67\n47 0x7f3f52694259p PyEval_EvalFrameEx + 13273\n48 0x7f3f526ec0dap PyEval_EvalCodeEx + 714\n49 0x7f3f5267de0fp\n50 0x7f3f526350e3p PyObject_Call + 67\n51 0x7f3f4f3e789fp\n52 0x7f3f526350e3p PyObject_Call + 67\n53 0x7f3f5269680ep PyEval_EvalFrameEx + 22926\n54 0x7f3f526ec0dap PyEval_EvalCodeEx + 714\n55 0x7f3f52696dbfp PyEval_EvalFrameEx + 24383\n56 0x7f3f526ec0dap PyEval_EvalCodeEx + 714\n57 0x7f3f52696dbfp PyEval_EvalFrameEx + 24383\n58 0x7f3f526ec0dap PyEval_EvalCodeEx + 714\n59 0x7f3f52696dbfp PyEval_EvalFrameEx + 24383\n60 0x7f3f526ec0dap PyEval_EvalCodeEx + 714\n61 0x7f3f5269697fp PyEval_EvalFrameEx + 23295\n62 0x7f3f526ec0dap PyEval_EvalCodeEx + 714\n63 0x7f3f5270b30ap PyEval_EvalCode + 26\n64 0x7f3f52695fcfp PyEval_EvalFrameEx + 20815\n65 0x7f3f526ec0dap PyEval_EvalCodeEx + 714\n66 0x7f3f5269697fp PyEval_EvalFrameEx + 23295\n67 0x7f3f526ec0dap PyEval_EvalCodeEx + 714\n68 0x7f3f5267de0fp\n69 0x7f3f526350e3p PyObject_Call + 67\n70 0x7f3f526f3a14p\n71 0x7f3f526f3ec0p Py_Main + 1088\n72 0x7f3f52a1106bp __libc_start_main + 235\n73 0x55b3f4e5077ap _start + 42\n" 1243 | ] 1244 | } 1245 | ], 1246 | "source": [ 1247 | "exe.run(program=new_main, fetch_list=[predict], feed=feed) # Not init" 1248 | ] 1249 | }, 1250 | { 1251 | "cell_type": "code", 1252 | "execution_count": 10, 1253 | "metadata": { 1254 | "ExecuteTime": { 1255 | "end_time": "2018-07-18T10:07:44.530499Z", 1256 | "start_time": "2018-07-18T10:07:44.522520Z" 1257 | } 1258 | }, 1259 | "outputs": [ 1260 | { 1261 | "data": { 1262 | "text/plain": [ 1263 | "[]" 1264 | ] 1265 | }, 1266 | "execution_count": 10, 1267 | "metadata": {}, 1268 | "output_type": "execute_result" 1269 | } 1270 | ], 1271 | "source": [ 1272 | "exe.run(new_startup)" 1273 | ] 1274 | }, 1275 | { 1276 | "cell_type": "code", 1277 | "execution_count": 11, 1278 | "metadata": { 1279 | "ExecuteTime": { 1280 | "end_time": "2018-07-18T10:07:45.002335Z", 1281 | "start_time": "2018-07-18T10:07:44.960028Z" 1282 | } 1283 | }, 1284 | "outputs": [ 1285 | { 1286 | "data": { 1287 | "text/plain": [ 1288 | "[array([[-0.2820843 ],\n", 1289 | " [-0.71883655],\n", 1290 | " [-0.29947978]], dtype=float32)]" 1291 | ] 1292 | }, 1293 | "execution_count": 11, 1294 | "metadata": {}, 1295 | "output_type": "execute_result" 1296 | } 1297 | ], 1298 | "source": [ 1299 | "exe.run(program=new_main, fetch_list=[predict], feed=feed)" 1300 | ] 1301 | }, 1302 | { 1303 | "cell_type": "code", 1304 | "execution_count": null, 1305 | "metadata": { 1306 | "ExecuteTime": { 1307 | "end_time": "2018-07-18T10:07:03.780371Z", 1308 | "start_time": "2018-07-18T10:07:03.756944Z" 1309 | } 1310 | }, 1311 | "outputs": [], 1312 | "source": [] 1313 | } 1314 | ], 1315 | "metadata": { 1316 | "kernelspec": { 1317 | "display_name": "Python 2", 1318 | "language": "python", 1319 | "name": "python2" 1320 | }, 1321 | "language_info": { 1322 | "codemirror_mode": { 1323 | "name": "ipython", 1324 | "version": 2 1325 | }, 1326 | "file_extension": ".py", 1327 | "mimetype": "text/x-python", 1328 | "name": "python", 1329 | "nbconvert_exporter": "python", 1330 | "pygments_lexer": "ipython2", 1331 | "version": "2.7.15" 1332 | } 1333 | }, 1334 | "nbformat": 4, 1335 | "nbformat_minor": 2 1336 | } 1337 | --------------------------------------------------------------------------------