vivek9 commited on
Commit
4c7b724
·
verified ·
1 Parent(s): 4019908

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +87 -1
app.py CHANGED
@@ -1,10 +1,96 @@
1
  import gradio as gr
 
 
 
 
 
2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
 
4
  def greet(name):
5
  return "Hello " + name
6
 
7
 
8
- demo = gr.Interface(fn=greet, inputs="text", outputs="text")
9
 
10
  demo.launch()
 
1
  import gradio as gr
2
+ import numpy as np
3
+ import networkx as nx
4
+ import matplotlib.pyplot as plt
5
+ import pandas as pd
6
+ from sklearn.metrics import log_loss
7
 
8
+ def sigmoid(z):
9
+ return 1.0/(1.0+ np.exp(-z))
10
+
11
+ def sigmoid_prime(z):
12
+ return sigmoid(z)*(1.0-sigmoid(z))
13
+
14
+ def cross_entropy(weights,bias,X_train,y_train):
15
+ return log_loss(y_train,[feedforward(X_train[k],bias,weights).reshape(-1,) for k in range(len(X_train))],labels=[1.0,0.0])
16
+
17
+ def feedforward(x,bias,weights):
18
+ for b,w in zip(bias,weights):
19
+ x=sigmoid(np.dot(w,x)+b)
20
+ return x
21
+
22
+
23
+ def update_mini_batch(weights,bias,X_mini,y_mini,eta):
24
+ derivative_b=[np.zeros(b.shape) for b in bias]
25
+ derivative_w=[np.zeros(w.shape) for w in weights]
26
+ for i in range(len(y_mini)):
27
+ x_mi=X_mini[i]
28
+ y_mi=y_mini[i]
29
+ derivative_b_sample, derivative_w_sample=backprop(weights,bias,x_mi,y_mi)
30
+ for j in range(len(derivative_b)):
31
+ derivative_b[j]+=derivative_b_sample[j]
32
+ derivative_w[j]+=derivative_w_sample[j]
33
+ for i in range(len(weights)):
34
+ weights[i]-=(eta/len(y_mini))*derivative_w[i]
35
+ bias[i]-=(eta/len(y_mini))*derivative_b[i]
36
+ return (weights,bias)
37
+
38
+ input_neurons = 10
39
+ hidden_neurons = 2
40
+ output_neurons = 1
41
+ weights_input_hidden = np.array([[ -7.22321659, -11.15471068, -30.00484398, 13.08466657,
42
+ -15.0766471 , 14.81645208, -13.11446119, 29.95009006,
43
+ 10.9668077 , 7.11140227],
44
+ [ 7.48857155, 11.20225432, 30.07325613, -13.1441687 ,
45
+ 15.08768163, -15.10126949, 13.11893102, -30.1417512 ,
46
+ -11.18918466, -7.44585737]]).T
47
+ biases_hidden = np.array([[1.62426502],
48
+ [1.63230885]]).reshape(-1)
49
+ weights_hidden_output = np.array([[23.73266741, 23.84381938]]).T
50
+ bias_output = np.array([[-35.80629779]]).reshape(-1)
51
+
52
+ def visualize_neural(a):
53
+ buffer = io.BytesIO()
54
+ input_values = [int(i) for i in list(a)]
55
+ hidden_layer_input = np.dot(input_values, weights_input_hidden) + biases_hidden
56
+ hidden_layer_output = np.array([round(i,3) for i in sigmoid(hidden_layer_input)])
57
+ output_layer_input = np.dot(hidden_layer_output, weights_hidden_output) + bias_output
58
+ output = sigmoid(output_layer_input)
59
+ G = nx.DiGraph()
60
+ for i in range(input_neurons):
61
+ G.add_node(f'{i}={input_values[i]}', pos=(0, i))
62
+ for i in range(hidden_neurons):
63
+ G.add_node(f'{i},f({np.round(hidden_layer_input[i],3)})={np.round(hidden_layer_output[i],3)}', pos=(1, 0.5*(input_neurons-1-hidden_neurons)+i))
64
+ for i in range(output_neurons):
65
+ G.add_node(f'Output {i+1}={output}', pos=(2, 4))
66
+ for i in range(input_neurons):
67
+ for j in range(hidden_neurons):
68
+ weight = weights_input_hidden[i][j]
69
+ G.add_edge(f'{i}={input_values[i]}', f'{j},f({np.round(hidden_layer_input[j],3)})={np.round(hidden_layer_output[j],3)}', weight=f"W[{j,i}]={weight}")
70
+ for i in range(hidden_neurons):
71
+ for j in range(output_neurons):
72
+ weight = weights_hidden_output[i][j]
73
+ G.add_edge(f'{i},f({np.round(hidden_layer_input[i],3)})={np.round(hidden_layer_output[i],3)}', f'Output {j+1}={output}', weight=weight)
74
+ pos = nx.get_node_attributes(G, 'pos')
75
+ plt.figure(figsize=(10, 5),dpi=200)
76
+ nx.draw(G, pos, with_labels=True, node_size=1e+3, node_color='lightblue', font_size=3, font_weight='bold')
77
+ labels = nx.get_edge_attributes(G, 'weight')
78
+ nx.draw_networkx_edge_labels(G, pos, edge_labels=labels,font_size=3,label_pos=0.8)
79
+ plt.title('Neural Network Graph')
80
+ plt.savefig(buffer, format='png')
81
+ plt.close()
82
+ buffer.seek(0)
83
+ image = Image.open(buffer)
84
+ image_array = np.array(image)
85
+ if output<0.5:
86
+ return image_array,"Non palindrom"
87
+ else:
88
+ return image_array,"palindrom"
89
 
90
  def greet(name):
91
  return "Hello " + name
92
 
93
 
94
+ demo = gr.Interface(fn=visualize_neural, inputs="text", outputs=[gr.Image(label="Neural network visualization"),gr.Textbox(label="output")])
95
 
96
  demo.launch()