<jupyter_start><jupyter_text>构造神经网络模型对两条曲线数据点进行二分类，详细信息参考博客https://huizhuzhao.github.io/2017/01/16/neural-networks-manifolds-topology.html<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

from sklearn.metrics import roc_curve, auc
import keras
from keras.models import Sequential
from keras.layers import Dense
from keras.callbacks import ModelCheckpoint, CSVLogger

def one_hot(index, dim):
    index = np.asarray(index, dtype=np.int32)
    I = np.eye(dim)
    res = I[index]
    return res

def dataset(n_samples, n_repeat=1):
    x1 = np.random.uniform(-1., 1., size=(n_samples, 1))
    y1 = x1**2
    
    x2 = np.random.uniform(-1., 1., size=(n_samples, 1))
    y2 = x2**2 + 0.2
    
    X_1 = np.concatenate([x1, y1], axis=1)
    X_2 = np.concatenate([x2, y2], axis=1)
    y_1 = one_hot(np.zeros(shape=(n_samples,)), 2)
    y_2 = one_hot(np.ones(shape=(n_samples,)), 2)
    
    X = np.concatenate([X_1, X_2], axis=0)
    y = np.concatenate([y_1, y_2], axis=0)
    X = np.tile(X, (n_repeat, 1))
    y = np.tile(y, (n_repeat, 1))
    
    return X, y

def build_model():
    model = Sequential()
    model.add(Dense(units=13, input_shape=(2, ), activation='relu')) # changing output_dim, you test different model
    model.add(Dense(output_dim=3, activation='relu'))
    model.add(Dense(units=2, activation='softmax'))
    model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
    return model

def get_callbacks():
    logger = CSVLogger('./logger.csv', append=False)
    hdf5_path = './weights.{epoch:02d}-{val_loss:.2f}.hdf5'
    checkpoint = ModelCheckpoint(hdf5_path, monitor='val_loss', save_best_only=True, verbose=1)
    return [logger, checkpoint]
train_X, train_y = dataset(1000, 100)
valid_X, valid_y = dataset(1000)
callbacks = get_callbacks()
model = build_model()
model.fit(train_X, train_y, epochs=20, batch_size=32, validation_data=(valid_X, valid_y), callbacks=callbacks)
train_X.shape, train_y.shape
def visualize_model(model, x_min, x_max, y_min, y_max, h):
    xx, yy = np.meshgrid(np.linspace(x_min, x_max, h), np.linspace(y_min, y_max, h))
    print(xx.shape, yy.shape)
    zz = model.predict(np.c_[xx.ravel(), yy.ravel()])
    print(zz.shape)
    zz = np.reshape(zz.argmax(axis=1), xx.shape)
    print(zz.shape)
    
    
    plt.contourf(xx, yy, zz, cmap=plt.cm.coolwarm, alpha=0.7)
    plt.scatter(valid_X[:, 0], valid_X[:, 1], c=valid_y.argmax(axis=1), cmap=plt.cm.coolwarm)
    plt.show()
   
visualize_model(model, -1., 1., 0., 1.5, 100)
csv_path = './logger.csv'
df = pd.read_csv(csv_path)
df.shape
plt.plot(df.epoch, df.val_acc)
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15974560419004863, 'fraction_numerical': 0.03292181069958848, 'mean_word_length': 4.25343811394892, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '239392', 'license': 'MIT', 'n_tokens_mistral': 1146, 'n_tokens_neox': 1104, 'n_words': 203, 'path': 'tests/nn_manifold_topology.ipynb', 'repo_name': 'huizhuzhao/jupyter_notebook', 'size': '2673'}