当前位置:网站首页>Dataloader source code_ DataLoader
Dataloader source code_ DataLoader
2022-06-30 23:11:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm your friend, Quan Jun .
import paddle.fluid as fluid
import numpy as np
BATCH_NUM = 10
BATCH_SIZE = 16
EPOCH_NUM = 4
CLASS_NUM = 10
ITERABLE = True # whether the created DataLoader object is iterable
USE_GPU = False # whether to use GPU
DATA_FORMAT = ‘batch_generator’ # data format of data source user provides
def simple_net(image, label):
fc_tmp = fluid.layers.fc(image, size=CLASS_NUM)
cross_entropy = fluid.layers.softmax_with_cross_entropy(image, label)
loss = fluid.layers.reduce_mean(cross_entropy)
sgd = fluid.optimizer.SGD(learning_rate=1e-3)
sgd.minimize(loss)
return loss
def get_random_images_and_labels(image_shape, label_shape):
image = np.random.random(size=image_shape).astype(‘float32’)
label = np.random.random(size=label_shape).astype(‘int64’)
return image, label
# If the data generator yields one sample each time,
# use DataLoader.set_sample_generator to set the data source.
def sample_generator_creator():
def __reader__():
for _ in range(BATCH_NUM * BATCH_SIZE):
image, label = get_random_images_and_labels([784], [1])
yield image, label
return __reader__
# If the data generator yield list of samples each time,
# use DataLoader.set_sample_list_generator to set the data source.
def sample_list_generator_creator():
def __reader__():
for _ in range(BATCH_NUM):
sample_list = []
for _ in range(BATCH_SIZE):
image, label = get_random_images_and_labels([784], [1])
sample_list.append([image, label])
yield sample_list
return __reader__
# If the data generator yields a batch each time,
# use DataLoader.set_batch_generator to set the data source.
def batch_generator_creator():
def __reader__():
for _ in range(BATCH_NUM):
batch_image, batch_label = get_random_images_and_labels([BATCH_SIZE, 784], [BATCH_SIZE, 1])
yield batch_image, batch_label
return __reader__
# If DataLoader is iterable, use for loop to train the network
def train_iterable(exe, prog, loss, loader):
for _ in range(EPOCH_NUM):
for data in loader():
exe.run(prog, feed=data, fetch_list=[loss])
# If DataLoader is not iterable, use start() and reset() method to control the process
def train_non_iterable(exe, prog, loss, loader):
for _ in range(EPOCH_NUM):
loader.start() # call DataLoader.start() before each epoch starts
try:
while True:
exe.run(prog, fetch_list=[loss])
except fluid.core.EOFException:
loader.reset() # call DataLoader.reset() after catching EOFException
def set_data_source(loader, places):
if DATA_FORMAT == ‘sample_generator’:
loader.set_sample_generator(sample_generator_creator(), batch_size=BATCH_SIZE, drop_last=True, places=places)
elif DATA_FORMAT == ‘sample_list_generator’:
loader.set_sample_list_generator(sample_list_generator_creator(), places=places)
elif DATA_FORMAT == ‘batch_generator’:
loader.set_batch_generator(batch_generator_creator(), places=places)
else:
raise ValueError(‘Unsupported data format’)
image = fluid.layers.data(name=’image’, shape=[784], dtype=’float32′)
label = fluid.layers.data(name=’label’, shape=[1], dtype=’int64′)
# Define DataLoader
loader = fluid.io.DataLoader.from_generator(feed_list=[image, label], capacity=16, iterable=ITERABLE)
# Define network
loss = simple_net(image, label)
# Set data source of DataLoader
#
# If DataLoader is iterable, places must be given and the number of places must be the same with device number.
# – If you are using GPU, call `fluid.cuda_places()` to get all GPU places.
# – If you are using CPU, call `fluid.cpu_places()` to get all CPU places.
#
# If DataLoader is not iterable, places can be None.
places = fluid.cuda_places() if USE_GPU else fluid.cpu_places()
set_data_source(loader, places)
exe = fluid.Executor(places[0])
exe.run(fluid.default_startup_program())
prog = fluid.CompiledProgram(fluid.default_main_program()).with_data_parallel(loss_name=loss.name)
if loader.iterable:
train_iterable(exe, prog, loss, loader)
else:
train_non_iterable(exe, prog, loss, loader)
”’
Users can use return_list = True in dygraph mode.
”’
with fluid.dygraph.guard(places[0]):
loader = fluid.io.DataLoader.from_generator(capacity=2, return_list=True)
set_data_source(loader, places[0])
for image, label in loader():
relu = fluid.layers.relu(image)
assert image.shape == [BATCH_SIZE, 784]
assert label.shape == [BATCH_SIZE, 1]
assert relu.shape == [BATCH_SIZE, 784]
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/132199.html Link to the original text :https://javaforall.cn
边栏推荐
- 【Android,Kotlin,TFLite】移动设备集成深度学习轻模型TFlite(物体检测篇)
- 一次革命、两股力量、三大环节:《工业能效提升行动计划》背后的“减碳”路线图
- [Android, kotlin, tflite] mobile device integration depth learning light model tflite (image classification)
- C language array interception, C string by array interception method (c/s)
- Solution to the conflict between unique index and logical deletion
- 请指教同花顺软件究竟是什么?另外想问,现在在线开户安全么?
- In depth analysis of Apache bookkeeper series: Part 4 - back pressure
- 软件测试报告包含哪些内容?如何获取高质量软件测试报告?
- 零样本和少样本学习
- Introduction to machine learning compilation course learning notes lesson 2 tensor program abstraction
猜你喜欢

Swift5.0 ----Swift FrameWork的创建及使用

Braces on the left of latex braces in latex multiline formula

Introduction to machine learning compilation course learning notes lesson 2 tensor program abstraction

76页智慧物流园区综合解决方案2022(附下载)

未来十年世界数字化与机器智能展望

In depth analysis of Apache bookkeeper series: Part 4 - back pressure

Redis - 01 cache: how to use read cache to improve system performance?

Redis' transaction and locking mechanism

企业出海数字化转型解决方案介绍

唯一性索引与逻辑删除冲突问题解决思路
随机推荐
New trends of China's national tide development in 2022
Ride: get picture Base64
Reason why wechat payment wxpaypubhelper V3 callback XML is empty
Deployment of microservices based on kubernetes platform
How do I open a stock account on my mobile phone? In addition, is it safe to open a mobile account?
As the public cloud market enters the deep water, can the calm Amazon cloud still sit still?
Architecture of IM integrated messaging system sharing 100000 TPS
"More Ford, more China" saw through the clouds, and the orders of Changan Ford's flagship products exceeded 10000
多線程經典案例
有孚网络混合云,加速企业数字化转型升级
Prospects of world digitalization and machine intelligence in the next decade
Cesiumjs 2022 ^ source code interpretation [6] - new architecture of modelempirical
异步过渡方案—Generator
如何区分平台安全和网上炒作?网络投机有哪些止损技巧?
CTFSHOW权限维持篇
Swift 5.0 - creation and use of swift framework
Spark - understand partitioner in one article
零样本和少样本学习
ESP8266 成为客户端和服务器
Shell multitasking to download video at the same time