tensorflow 将模拟数据制作成内存对象数据集

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)    # 为-1到1之间连续的100个浮点数
    x_batch = tf.cast(x_batch, tf.float32)
#     print("*x_batch.shape", *x_batch.shape)
    y_batch = 2 * x_batch + np.random.randn(x_batch.shape[0]) * 0.3     # y=2x,但是加入了噪声
    y_batch = tf.cast(y_batch, tf.float32)
    
    yield x_batch, y_batch        # 以生成器的方式返回
# 1.循环获取数据
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())
# 2.显示一组数据
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)    # 为-1到1之间连续的100个浮点数
    #     print("*x_batch.shape", *x_batch.shape)
        y_batch = 2 * x_batch + np.random.randn(x_batch.shape[0]) * 0.3     # y=2x,但是加入了噪声

        yield shuffle(x_batch, y_batch), i        # 以生成器的方式返回
# 1.循环获取数据
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())
# 2.显示一组数据
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()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

廷益--飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值