<jupyter_start><jupyter_text>Generate Dataset<jupyter_code>N = 100 # number of points per class
D = 2 # dimensionality
K = 3 # number of classes
seed = 4 #keep seed
X = np.zeros((N*K,D)) # data matrix (each row = single example)
y = np.zeros(N*K, dtype='uint8') # class labels
np.random.seed(seed) #fixing random 
for j in range(K):
    ix = range(N*j,N*(j+1))
    r = np.linspace(0.0,1,N) # radius
    t = np.linspace(j*4,(j+1)*4,N) + np.random.randn(N)*0.2 # theta
    X[ix] = np.c_[r*np.sin(t), r*np.cos(t)]
    y[ix] = j
X[:, 0] += 1
X[:, 1] += 2
# lets visualize the data:
plt.figure(figsize=(10,8))
plt.scatter(X[:, 0], X[:, 1], c=y, s=40, cmap=plt.cm.Spectral, edgecolors="black")
ax = plt.gca()
ax.set_aspect("equal")
plt.show()<jupyter_output><empty_output><jupyter_text>Plot function for ploting decision boundary<jupyter_code>def plot(h=0.02):
    x_min, x_max = X[:, 0].min() - 0.5, X[:, 0].max() + 0.5
    y_min, y_max = X[:, 1].min() - 0.5, X[:, 1].max() + 0.5
    xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
                 np.arange(y_min, y_max, h))
    Z = sess.run(t_prediction, feed_dict={t_x: np.column_stack([xx.ravel(), yy.ravel()])})
    Z = Z.reshape(xx.shape)

    plt.figure(figsize=(10,8))
    plt.contourf(xx, yy, Z, cmap=plt.cm.Spectral, alpha=0.8)
    plt.scatter(X[:, 0], X[:, 1], c=y, s=40, cmap=plt.cm.Spectral, edgecolors="black")
    plt.xlim(xx.min(), xx.max())
    plt.ylim(yy.min(), yy.max())
    plt.gca().set_aspect("equal")
    plt.show()<jupyter_output><empty_output><jupyter_text>Creating input placeholders and model trainable parameters<jupyter_code>t_x = tf.placeholder(tf.float32, shape=[None, D])
t_y = tf.placeholder(tf.int32, shape=[None])
W = tf.Variable(tf.random_normal([D, K]))
b = tf.Variable(tf.zeros([1, K], dtype=tf.float32))<jupyter_output><empty_output><jupyter_text>some hyperparameters<jupyter_code>step_size = 1e-0
reg = 1e-3 # regularization strength<jupyter_output><empty_output><jupyter_text>Creating main logic<jupyter_code>t_scores = tf.matmul(t_x, W) + b
t_prediction = tf.argmax(t_scores, axis=-1, output_type=tf.int32)
t_acc = tf.reduce_mean(tf.cast(tf.equal(t_prediction, t_y), tf.float32))
t_loss_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(
    labels=t_y, logits=t_scores)
t_loss_reg = tf.reduce_sum(W * W) * reg
t_loss = tf.reduce_mean(t_loss_entropy + t_loss_reg)

optimizer = tf.train.GradientDescentOptimizer(learning_rate=step_size)

train_op = optimizer.minimize(t_loss)<jupyter_output><empty_output><jupyter_text>Close opened sessions<jupyter_code>if "sess" in dir():
    sess.close()<jupyter_output><empty_output><jupyter_text>Open new session and initialize all variables<jupyter_code>sess = tf.Session()
sess.run(tf.global_variables_initializer())
plot()<jupyter_output><empty_output><jupyter_text>Train our model<jupyter_code>for i in range(200):
    loss, acc, _ = sess.run([t_loss, t_acc, train_op], feed_dict={t_x:X, t_y:y})
    
    if i % 2 == 0:
        print("iteration %d:\tloss %f\tacc %f" % (i, loss, acc))
        clear_output(wait=True)
        plot(h=0.01)<jupyter_output><empty_output><jupyter_text>Evaluate training set accuracy<jupyter_code>print('training accuracy: %.2f%%' % 
      (100*sess.run(t_acc, feed_dict={t_x:X, t_y:y})))<jupyter_output>training accuracy: 49.67%
{'fraction_non_alphanumeric': 0.1763277693474962, 'fraction_numerical': 0.025189681335356602, 'mean_word_length': 5.207156308851224, 'pattern_counts': {'":': 0, '<': 36, '<?xml version=': 0, '>': 36, 'https://': 0, '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': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '218776', 'license': 'MIT', 'n_tokens_mistral': 1449, 'n_tokens_neox': 1388, 'n_words': 237, 'path': 'Tensorflow/Simple_models/Linear_Softmax_Classifier.ipynb', 'repo_name': 'GarikAdamyan/ISTC_ML_course', 'size': '3295'}