tensorflow 将模拟数据制作成内存对象数据集
import tensorflow as tf
import numpy as np
import matplotlib. pyplot as plt
% matplotlib inline
print ( tf. __version__)
print ( np. __version__)
def generate_data ( batch_size= 100 ) :
"""y = 2x 函数数据生成器"""
x_batch = np. linspace( - 1 , 1 , batch_size)
x_batch = tf. cast( x_batch, tf. float32)
y_batch = 2 * x_batch + np. random. randn( x_batch. shape[ 0 ] ) * 0.3
y_batch = tf. cast( y_batch, tf. float32)
yield x_batch, y_batch
train_epochs = 10
for epoch in range ( train_epochs) :
for x_batch, y_batch in generate_data( ) :
print ( epoch, "| x.shape:" , x_batch. shape, "| x[:3]:" , x_batch[ : 3 ] . numpy( ) )
print ( epoch, "| y.shape:" , y_batch. shape, "| y[:3]:" , y_batch[ : 3 ] . numpy( ) )
train_data = list ( generate_data( ) ) [ 0 ]
plt. plot( train_data[ 0 ] , train_data[ 1 ] , 'ro' , label= 'Original data' )
plt. legend( )
plt. show( )
添加迭代器
import tensorflow as tf
import numpy as np
import matplotlib. pyplot as plt
from sklearn. utils import shuffle
% matplotlib inline
print ( tf. __version__)
print ( np. __version__)
def generate_data ( epochs, batch_size= 100 ) :
"""y = 2x 函数数据生成器 增加迭代器"""
for i in range ( epochs) :
x_batch = np. linspace( - 1 , 1 , batch_size)
y_batch = 2 * x_batch + np. random. randn( x_batch. shape[ 0 ] ) * 0.3
yield shuffle( x_batch, y_batch) , i
train_epochs = 10
for ( x_batch, y_batch) , epoch_index in generate_data( train_epochs) :
x_batch = tf. cast( x_batch, tf. float32)
y_batch = tf. cast( y_batch, tf. float32)
print ( epoch_index, "| x.shape:" , x_batch. shape, "| x[:3]:" , x_batch[ : 3 ] . numpy( ) )
print ( epoch_index, "| y.shape:" , y_batch. shape, "| y[:3]:" , y_batch[ : 3 ] . numpy( ) )
train_data = list ( generate_data( 1 ) ) [ 0 ]
plt. plot( train_data[ 0 ] [ 0 ] , train_data[ 0 ] [ 1 ] , 'ro' , label= 'Original data' )
plt. legend( )
plt. show( )