# Daterset

```python
import tensorflow as tf
import numpy as np
from tensorflow.contrib.data import Dataset
import matplotlib.pyplot as plt

# load your data or create your data in here
npx = np.random.uniform(-1, 1, (1000, 1))                           # x data
npy = np.power(npx, 2) + np.random.normal(0, 0.1, size=npx.shape)   # y data
npx_train, npx_test = np.split(npx, [800])                          # training and test data
npy_train, npy_test = np.split(npy, [800])

# use placeholder, later you may need different data, pass the different data into placeholder
tfx = tf.placeholder(npx_train.dtype, npx_train.shape)
tfy = tf.placeholder(npy_train.dtype, npy_train.shape)

# create dataloader
dataset = tf.data.Dataset.from_tensor_slices((tfx, tfy))
dataset = dataset.shuffle(buffer_size=1000)   # choose data randomly from this buffer
dataset = dataset.batch(32)                   # batch size you will use
dataset = dataset.repeat(3)                   # repeat for 3 epochs
iterator = dataset.make_initializable_iterator()  # later we have to initialize this one

# your network
bx, by = iterator.get_next()                  # use batch to update
l1 = tf.layers.dense(bx, 10, tf.nn.relu)
out = tf.layers.dense(l1, npy.shape[1])
loss = tf.losses.mean_squared_error(by, out)
train = tf.train.GradientDescentOptimizer(0.1).minimize(loss)

sess = tf.Session()
# need to initialize the iterator in this case
sess.run([iterator.initializer, tf.global_variables_initializer()], feed_dict={tfx: npx_train, tfy: npy_train})
train_losses,test_losses=[],[]
for step in range(201):
    try:
        _, trainl = sess.run([train, loss])                       # train

        # if step % 10 == 0:
        testl = sess.run(loss, {bx: npx_test, by: npy_test})    # test
        train_losses.append(trainl)
        test_losses.append(testl)
          # print('step: %i/200' % step, '|train loss:', trainl, '|test loss:', testl)
    except tf.errors.OutOfRangeError:     # if training takes more than 3 epochs, training will be stopped
        print('Finish the last epoch.')
        break
print(step)
plt.plot(list(range(step)),train_losses,color='r',label='train loss')
plt.plot(list(range(step)),test_losses,color='b',label='test loss')
plt.legend(loc='best')
plt.show()
```

![](https://3919171906-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Le0cHhONfYy30rElIum%2F-Le0cK-U6Aa_6hpASzPT%2F-Le0cKfMhyzZZlNd2jS3%2FFigu2re_1.png?generation=1556953023879199\&alt=media)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://baozoulin.gitbook.io/tensorflow/jian-zao-di-yi-ge-shen-jing-wang-luo/daterset.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
