def draw_mol_with_property(mol, property, **kwargs): """ http://rdkit.blogspot.com/2015/02/new-drawing-code.html Parameters --------- property : dict key atom idx, val the property (need to be stringfiable) """ import copy from rdkit.Chem import Draw from rdkit.Chem import AllChem def run_from_ipython(): try: __IPYTHON__ return True except NameError: return False AllChem.Compute2DCoords(mol) mol = copy.deepcopy(mol) #FIXME do I really need deepcopy? for idx in property: # opts.atomLabels[idx] = mol.GetAtomWithIdx(idx).SetProp('molAtomMapNumber', "({})".format(str(property[idx]))) mol = Draw.PrepareMolForDrawing(mol, kekulize=False) #enable adding stereochem if run_from_ipython(): from IPython.display import SVG, display if "width" in kwargs and type( kwargs["width"]) is int and "height" in kwargs and type( kwargs["height"]) is int: drawer = Draw.MolDraw2DSVG(kwargs["width"], kwargs["height"]) else: drawer = Draw.MolDraw2DSVG(500, 250) drawer.DrawMolecule(mol) drawer.FinishDrawing() display(SVG(drawer.GetDrawingText().replace("svg:", ""))) else: if "width" in kwargs and type( kwargs["width"]) is int and "height" in kwargs and type( kwargs["height"]) is int: drawer = Draw.MolDraw2DCairo(kwargs["width"], kwargs["height"]) else: drawer = Draw.MolDraw2DCairo(500, 250) #cairo requires anaconda rdkit # opts = drawer.drawOptions() drawer.DrawMolecule(mol) drawer.FinishDrawing() # # with open("/home/shuwang/sandbox/tmp.png","wb") as f: # f.write(drawer.GetDrawingText()) import io import matplotlib.pyplot as plt import matplotlib.image as mpimg buff = io.BytesIO() buff.write(drawer.GetDrawingText()) buff.seek(0) plt.figure() i = mpimg.imread(buff) plt.imshow(i) plt.show()
# Allow to use 'from . import <module>' when run as script (cf. PEP 366) if __name__ == '__main__' and __package__ is None: __package__ = 'debuggingbook' # Tours through the Book # ====================== if __name__ == '__main__': print('# Tours through the Book') if __name__ == '__main__': from IPython.display import SVG if __name__ == '__main__': SVG(filename='PICS/Sitemap.svg') ## The Pragmatic Programmer Tour ## ----------------------------- if __name__ == '__main__': print('\n## The Pragmatic Programmer Tour') ## The Young Researcher Tour ## ------------------------- if __name__ == '__main__': print('\n## The Young Researcher Tour') ## Lessons Learned ## ---------------
lay_7 = Conv2D(base_depth * 4, kernel_size=(3, 3), padding="same")(lay_6) lay_8 = Conv2D(base_depth * 4, kernel_size=(3, 3), padding="same")(lay_7) lay_9 = UpSampling2D((2, 2))(lay_8) lay_10 = concatenate([lay_5, lay_9]) lay_11 = Conv2D(base_depth * 2, kernel_size=(3, 3), padding="same")(lay_10) lay_12 = Conv2D(base_depth * 2, kernel_size=(3, 3), padding="same")(lay_11) lay_13 = UpSampling2D((2, 2))(lay_12) lay_14 = concatenate([lay_2, lay_13]) lay_15 = Conv2D(base_depth, kernel_size=(3, 3), padding="same")(lay_14) lay_16 = Conv2D(base_depth, kernel_size=(3, 3), padding="same")(lay_15) lay_17 = Conv2D(1, kernel_size=(1, 1), padding="same", activation="sigmoid")(lay_16) t_unet = Model(inputs=[in_img], outputs=[lay_17], name="SmallUNET") dot_mod = model_to_dot(t_unet, show_shapes=True, show_layer_names=False) dot_mod.set_rankdir("UD") SVG(dot_mod.create_svg()) # In[64]: t_unet.summary() # In[65]: import pandas as pd import numpy as np import matplotlib.pyplot as plt from skimage.io import imread cell_img = (imread("../common/data/em_image.png")[::2, ::2]) / 255.0 cell_seg = imread("../common/data/em_image_seg.png", as_gray=True)[::2, ::2] > 0
def dump_model(model): model.summary() return SVG(model_to_dot(model).create(prog='dot', format='svg'))
def MANvsIA(): board = chess.Board() display(SVG(chess.svg.board(board=board))) while not (board.is_game_over()): moves = board.legal_moves #The MAN start everytime man_move = chess.Move.from_uci(input("your move : ")) if man_move in moves: board.push(man_move) line.append(man_move) display(SVG(chess.svg.board(board=board, lastmove=man_move))) else: while man_move not in moves: print("Illegal move, please try an other one") print("Here is the list of legal move") for move in board.legal_moves: print(move) man_move = chess.Move.from_uci(input("your move : ")) board.push(man_move) line.append(man_move) display(SVG(chess.svg.board(board=board, lastmove=man_move))) #We look after every move if the game is over in order to end it if board.is_game_over(): print("The game is over") print(board.result()) game.headers["Result"] = board.result() print("") #Now it's IA turns moves = board.legal_moves #It look if the best_move function has a move to push #If not, we use alpha-beta method if best_move(board) == "Nothing": move = MinMax.minimaxRoot(3, board, True) line.append(move) board.push(move) display(SVG(chess.svg.board(board=board, lastmove=move))) print("") #If there is a move from polyglot, we push it elif best_move(board) in moves: moveToPush = best_move(board) line.append(moveToPush) board.push(moveToPush) display(SVG(chess.svg.board(board=board, lastmove=moveToPush))) print("") #We look after every move if the game is over in order to end it if board.is_game_over(): print("The game is over") print(board.result()) game.headers["Result"] = board.result() print("") ###Game saving ### game.add_line(line) new_pgn = open("D:/Polytech/FI 3/Proj/gitTheo2.0/savedGames.pgn", "a", encoding="utf-8") exporter = chess.pgn.FileExporter(new_pgn) game.accept(exporter) new_pgn.close()
def render_model(cobra_model, background_template=None, custom_css=None, figure_id=None, hide_unused=None, hide_unused_cofactors=None, inactive_alpha=1., figsize=None, label=None, fontsize=None, default_flux_width=2.5, flux_dict=None, metabolite_dict=None, svg_scale=100, flowLayout=False): """ Render a cobra.Model object in the current window Parameters: background_template: filename for an SVG to render behind the flux figure. Useful for compartments or layout guides. custom_css: Additional CSS to embed in the figure. Use HTML inspector to show labels and classes applied to reactions and metabolites. figure_id: Each figure in the page requires a unique ID, which can be passed or generated automatically. hide_unused: whether or not to show metabolites and reactions with zero flux. hide_unused_cofactors: similar to hide_unused, but only hide cofactor nodes for reactions with 0 flux. inactive_alpha: Alpha value with which to color reactions and nodes without any carried flux. Defaults to 1. figsize: size, in pixels, of the generated SVG window. Defaults to 1024x768. fontsize: text size, in pt. Defaults to 12 default_flux_width: If reaction fluxes are missing, the default thickness to use for connecting arrows. flux_dict: A dictionary-like object containing the desired fluxes for each reaction in the model metabolite_dict: A dictionary-like object containing the desired carried fluxes for each metabolite in the model """ # Increment figure counter # Get figure name and JSON string for the cobra model if not figure_id: render_model._fignum += 1 figure_id = 'd3flux{:0>3d}'.format(render_model._fignum) if not figsize: figsize = (1028, 768) modeljson = create_model_json(cobra_model, flux_dict, metabolite_dict) if not hide_unused: hide_unused = "false" else: hide_unused = "true" if not hide_unused_cofactors: hide_unused_cofactors = "false" else: hide_unused_cofactors = "true" # Handle custom CSS if not custom_css: custom_css = '' if not fontsize: fontsize = 12 # Handle background template if not background_template: background_svg = '' no_background = "true" else: from IPython.display import SVG background_svg = SVG(background_template).data no_background = "false" # Initialize the jinja templates env = Environment(loader=FileSystemLoader( os.path.join(os.path.dirname(d3flux.__file__), 'templates'))) template_css = env.get_template('network_style.css') template_html = env.get_template('output_template.html') template_js = env.get_template('d3flux.js') # Render the jinja templates with the given variables css = template_css.render(inactive_alpha=inactive_alpha, fontsize=fontsize, cf_fontsize=0.8 * fontsize) js = template_js.render(figure_id=figure_id, modeljson=modeljson, no_background=no_background, hide_unused=hide_unused, hide_unused_cofactors=hide_unused_cofactors, figwidth=figsize[0], figheight=figsize[1], css=compress(css + custom_css), default_flux_width=default_flux_width, svg_scale=svg_scale, flowLayout=flowLayout) html = template_html.render(figure_id=figure_id, background_svg=background_svg, javascript_source=js) # compile and return HTML return HTML(html)
fig = sg.SVGFigure( Unit((fig1_width_size + fig2_width_size) - 360), Unit(min(fig1_height_size, fig2_height_size) - 50), ) fig.append( [etree.Element("rect", {"width": "100%", "height": "100%", "fill": "white"})] ) plot1 = fig1.getroot() plot1.moveto(10, 30) plot2 = fig2.getroot() plot2.moveto(fig1_width_size - 160, 12) text_A = sg.TextElement(10, 30, "A", size=22, weight="bold") text_B = sg.TextElement(fig1_width_size - 160, 30, "B", size=22, weight="bold") fig.append([plot1, plot2, text_A, text_B]) # - # save generated SVG files fig.save("output/figures/polka_filtered_background_panels.svg") svg2png( bytestring=fig.to_str(), write_to="output/figures/polka_filtered_background_panels.png", dpi=600, ) display(SVG(fig.to_str()))
from .RailroadDiagrams import NonTerminal, Terminal, Choice, HorizontalChoice, Sequence from .RailroadDiagrams import show_diagram if __name__ == '__main__': from IPython.display import SVG def syntax_diagram_symbol(symbol): if is_nonterminal(symbol): return NonTerminal(symbol[1:-1]) else: return Terminal(symbol) if __name__ == '__main__': SVG(show_diagram(syntax_diagram_symbol('<term>'))) def syntax_diagram_expr(expansion): # In later chapters, we allow expansions to be tuples, # with the expansion being the first element if isinstance(expansion, tuple): expansion = expansion[0] symbols = [sym for sym in re.split(RE_NONTERMINAL, expansion) if sym != ""] if len(symbols) == 0: symbols = [""] # special case: empty expansion return Sequence(*[syntax_diagram_symbol(sym) for sym in symbols]) if __name__ == '__main__': SVG(show_diagram(syntax_diagram_expr(EXPR_GRAMMAR['<term>'][0])))
def syntax_diagram(grammar): from IPython.display import SVG, display for key in grammar: print("%s" % key[1:-1]) display(SVG(show_diagram(syntax_diagram_alt(grammar[key]))))
GAN.fit(x_train[0].reshape(1, 784), x_train[0].reshape((1, 784)), batch_size=30, nb_epoch=1, verbose=1) x_train_GAN = x_train[0].reshape(1, 784) a = GAN.predict(x_train[0].reshape(1, 784), verbose=1) plt.figure(figsize=(10, 10)) ax = plt.subplot(1, 2, 1) plt.imshow(x_train_GAN.reshape(28, 28)) plt.gray() ax.get_xaxis().set_visible(False) ax.get_yaxis().set_visible(False) ax = plt.subplot(1, 2, 2) plt.imshow(a.reshape(28, 28)) plt.gray() ax.get_xaxis().set_visible(False) ax.get_yaxis().set_visible(False) plt.show() import pydot import graphviz import pydot_ng as pydot from IPython.display import SVG from keras.utils.visualize_util import model_to_dot SVG(model_to_dot(recog_right).create(prog='dot', format='svg'))
def syntax_diagram(grammar): for key in grammar: print("%s" % key[1:-1]) display(SVG(show_diagram(syntax_diagram_alt(grammar[key]))))
def svg(self, line, cell): """Render the cell as an SVG literal""" display(SVG(cell))
# Define Model model = Model(inputs=[encoder_inputs, decoder_inputs], outputs=outputs, name='training_model') # Compile model.compile(optimizer=Adam(learning_rate=learning_late), loss=sparse_categorical_crossentropy) # Display Model Summary from IPython.display import SVG from tensorflow.keras.utils import model_to_dot # You need to install graphviz! (sudo apt install graphviz or brew install graphviz) SVG( model_to_dot(model, show_shapes=True, dpi=65).create(prog='dot', format='svg')) # In[16]: from sklearn.model_selection import train_test_split x_train, x_test, y_train, y_test = train_test_split(keywords, sentences, test_size=0.33) # In[17]: history = model.fit([x_train, y_train[:, :-1]], y_train[:, 1:], epochs=epochs,
model.add(Dense(50, # output neurons in layer input_dim=X.shape[1], # number of inputs activation='relu')) # activation function model.add(Dense(50, activation='relu')) # hidden layer model.add(Dense(1, activation='sigmoid')) # output layer model.summary() # Visualize a model # Requires graphviz from IPython.display import SVG from keras.utils.vis_utils import model_to_dot dot = model_to_dot(model, show_shapes=True, show_layer_names=False) SVG(dot.create(prog='dot', format='svg')) from keras.utils import plot_model plot_model(model, to_file='model.png') # fit the model model.compile(loss='binary_crossentropy', # cost function optimizer='adam', # use adam as the optimizer metrics=['accuracy']) # compute accuracy, for scoring model_info = model.fit(X, Y, epochs=5, validation_split=.2) # these are the learned coefficients model.get_weights()
def visualize_model(model): return SVG(model_to_dot(model).create(prog='dot', format='svg'))
for _ in tqdm(range(gen_size)): y_hat_node, y_hat_add, y_hat_edge, node, edge = generator() y_hat_node_batch = torch.cat((y_hat_node_batch, y_hat_node.view(1, N_Max_atom, -1)), 0) #y_hat_add_batch = torch.cat((y_hat_add_batch, y_hat_add.view(1, -1)), 0) y_hat_edge_batch = torch.cat((y_hat_edge_batch, y_hat_edge.view(1, N_Max_hand*N_Max_atom, -1)), 0) node_batch.append(node.tolist()) edge_batch.append(edge) mols_gen, smiles_gen, valid_rate_gen, failures_gen = write_mol(node_batch, edge_batch, gen_size) Show Generated Graph You can change index number (0 < index < len(mols_gen)) len(mols_gen) index = 0 SVG(moltosvg(mols_gen[index], molSize=(300,300), index=False)) Show Plot train_logs = pd.read_csv('./train_logs.csv') source_train = ColumnDataSource(train_logs) settings = dict(plot_width=480, plot_height=430, min_border=0) p = figure(title="Binary Cross Entropy Loss with Sigmoid Layer", x_axis_label="epoch",y_axis_label="Loss", **settings) p.line(x='epoch', y='loss', source=source_train, legend="Train") draw = show(p, notebook_handle=True) Find Optim Learning rate. (Option) Choice the learning rate before increasing loss. ##Optimal learning rates
if F_imnew<=F_imold: node.time=a #print("aa") else: c+=1 print(c) treeSequence = msprime.simulate(sample_size=SAMPLE_SIZE, Ne=Ne, length=LENGTH, recombination_rate=RECOMBINATION_RATE, mutation_rate=MUTATION_RATE) print( "\n\033[41m GENOMICS \033[0m\n") print("\n\033[43mSimulating trees\033[0m\n") for tree in treeSequence.trees(): print("tree {}: interval = {}".format(tree.index, tree.interval)) display(SVG(tree.draw(format="svg"))) print("\033[34mTotal branch length of the tree: {}\033[0m\n".format(tree.total_branch_length)) print("\n\033[40m" + "-" * 100 + "\033[0m\n") edges = {} nodes = {} for node in treeSequence.nodes(): nodes[node.id] = Node(node.id, node.time) for edge in treeSequence.edges(): edgeId = edge.id child = nodes[edge.child] parent = nodes[edge.parent] e = Edge( edgeId,
# preprocessing sentences into sentence vectors sentence = Input(shape=(T, embedding_size), name='Sentences') # batch, 50, 300 sentence_vec = Bidirectional(CuDNNGRU(units=n_a, return_sequences=False), name='Sentence_Vectors')(sentence) # batch, 300 # dot #product = Dot(axes=-1, normalize=False, name='Matrix')([word_vec, sentence_vec]) product = tf.matmul(word_vec, sentence_vec, transpose_b = True, name = 'Matrix') key_matrix = K.transpose(product) model = Model(inputs= sentence, outputs=key_matrix) return model # create a model Factorize = model(embedding_size, n_a) # get a summary of the model Factorize.summary() SVG(model_to_dot(Factorize, show_shapes=True, show_layer_names=True, rankdir='HB').create(prog='dot', format='svg')) plot_model(Factorize, to_file='model.png', show_shapes=True, show_layer_names=True) #%% Run Factorize.compile(loss='mean_squared_error', optimizer = Adam(lr=0.001), metrics=['mean_squared_error', 'mean_absolute_error']) hist = Factorize.fit(X_train_padded, key_matrix_train, batch_size=256, epochs=500, verbose=1, validation_data=(X_test_padded, key_matrix_test)) #%% Loss plot %matplotlib inline import matplotlib.pyplot as plt # accuracy
# In[4]: model = Sequential() model.add(Dense(8, input_dim=2, activation='sigmoid')) model.add(Dense(1, activation='sigmoid')) model.compile(loss='mean_squared_error', optimizer=SGD(lr=1), metrics=['accuracy']) # In[5]: from IPython.display import SVG from keras.utils.vis_utils import model_to_dot SVG( model_to_dot(model, show_shapes=True, rankdir='LR').create(prog='dot', format='svg')) # In[6]: epochs = 2000 model.fit(training_data, target_data, epochs=epochs) # In[7]: print "loss:", model.evaluate(x=training_data, y=target_data, verbose=0) print "" print model.predict(training_data) print "" print model.predict(training_data).round()
grid_result.best_params_ # 查看最优神经网络的超参数 # 如果你的模型需要12个小时或者一天的时间来训练,那么网格搜索可能需要花上一周甚至更长的时间。因此,神经网络自动超参数调校不是万能药,但是它在某些特定情形下是有用的。 # 20.14 可视化神经网络 from keras import models from keras import layers from IPython.display import SVG from keras.utils.vis_utils import model_to_dot from keras.utils import plot_model network = models.Sequential() network.add(layers.Dense(units=16, activation="relu", input_shape=(10, 1))) network.add(layers.Dense(units=16, activation="relu")) network.add(layers.Dense(units=1, activation="sigmoid")) SVG(model_to_dot(network, show_shapes=True).create(prog="dot", format="svg")) # 可视化网络结构 plot_model(network, show_shapes=True, to_file="network.png") # 将可视化后的网络结构图保存为文件 # Keras 提供了工具函数用于快速可视化神经网络。如果想在Jupyter Notebook 中显示一个神经网络,可以使用model_to_dot。show_shapes 参数指定 # 是否展示输入和输出的形状,它可以帮助我们调试网络。如果想展示一个更简单的模型,可以设置show_shapes=False SVG(model_to_dot(network, show_shapes=False).create(prog="dot", format="svg")) # 20.15 图像分类 import numpy as np from keras.datasets import mnist from keras.models import Sequential from keras.layers import Dense, Dropout, Flatten from keras.layers.convolutional import Conv2D, MaxPooling2D from keras.utils import np_utils
def _svg(g, as_polygon=True): """Format and show a Geo array, np.ndarray or list structure in SVG format. Notes ----- Geometry must be expected to form polylines or polygons. IPython required. >>> from IPython.display import SVG alternate colors: white, silver, gray black, red, maroon, purple, blue, navy, aqua, green, teal, lime, yellow, magenta, cyan """ def svg_path(g_bits, scale_by, o_f_s): """Make the svg from a list of 2d arrays""" opacity, fill_color, stroke = o_f_s pth = [" M {},{} " + "L {},{} " * (len(b) - 1) for b in g_bits] ln = [pth[i].format(*b.ravel()) for i, b in enumerate(g_bits)] pth = "".join(ln) + "z" s = ('<path fill-rule="evenodd" fill="{0}" stroke="{1}" ' 'stroke-width="{2}" opacity="{3}" d="{4}"/>').format( fill_color, stroke, 1.5 * scale_by, opacity, pth) return s # ---- msg0 = "\nImport error..\n>>> from IPython.display import SVG\nfailed." msg1 = "A Geo array or ndarray (with ndim >=2) is required." # ---- # Geo array, np.ndarray check try: from IPython.display import SVG except ImportError: print(dedent(msg0)) return None # ---- checks for Geo or ndarray. Convert lists, tuples to np.ndarray if isinstance(g, (list, tuple)): g = np.asarray(g) if ('Geo' in str(type(g))) & (issubclass(g.__class__, np.ndarray)): GA = True g_bits = g.bits elif isinstance(g, np.ndarray): GA = False if g.ndim == 2: g_bits = [g] L, B = g.min(axis=0) R, T = g.max(axis=0) elif g.ndim == 3: g_bits = [g[i] for i in range(g.shape[0])] L, B = g.min(axis=(0, 1)) R, T = g.max(axis=(0, 1)) elif g.dtype.kind == 'O': g_bits = [] for i, b in enumerate(g): b = np.array(b) if b.ndim == 2: g_bits.append(b) elif b.ndim == 3: g_bits.extend([b[i] for i in range(b.shape[0])]) L, B = np.min(np.vstack([np.min(i, axis=0) for i in g_bits]), axis=0) R, T = np.max(np.vstack([np.max(i, axis=0) for i in g_bits]), axis=0) else: print(msg1) return None else: print(msg1) return None # ---- # derive parameters if as_polygon: o_f_s = ["0.75", "red", "black"] # opacity, fill_color, stroke color else: o_f_s = ["1.0", "none", "red"] # ---- d_x, d_y = (R - L, T - B) hght = min([max([100., d_y]), 200]) width = int(d_x / d_y * hght) scale_by = max([d_x, d_y]) / max([width, hght]) # ---- # derive the geometry path pth_geom = svg_path(g_bits, scale_by, o_f_s) # ---- svg path string # construct the final output view_box = "{} {} {} {}".format(L, B, d_x, d_y) transform = "matrix(1,0,0,-1,0,{0})".format(T + B) hdr = '<svg xmlns="http://www.w3.org/2000/svg" ' \ 'xmlns:xlink="http://www.w3.org/1999/xlink" ' f0 = 'width="{}" height="{}" viewBox="{}" '.format(width, hght, view_box) f1 = 'preserveAspectRatio="xMinYMin meet">' f2 = '<g transform="{}">{}</g></svg>'.format(transform, pth_geom) s = hdr + f0 + f1 + f2 if GA: # Geo array display g.SVG = s return SVG(g.SVG) # plot the representation else: # np.ndarray display return SVG(s)
def _repr_svg_(self): "Displays SVG of workflow in Jupyter notebook." from IPython.display import SVG, display display(SVG(url=(self.path_to_knime_workflow / "workflow.svg").as_uri()))
def show_model(model): return SVG(model_to_dot(model, show_shapes=True).create(prog='dot', format='svg'))
x = MaxPooling2D((2, 2), border_mode='same')(x) x = Convolution2D(128, 3, 3, activation='relu', border_mode='same')(x) x = MaxPooling2D((2, 2), border_mode='same')(x) x = Flatten()(x) encoded = Dense(2000)(x) oneD = Dense(BaseLevel*BaseLevel*128)(encoded) fold = Reshape((BaseLevel,BaseLevel,128))(oneD) x = UpSampling2D((2, 2))(fold) x = Convolution2D(128, 3, 3, activation='relu', border_mode='same')(x) x = UpSampling2D((2, 2))(x) decoded = Convolution2D(3, 3, 3, activation='sigmoid', border_mode='same')(x) autoencoder = Model(input_img, decoded) autoencoder.compile(optimizer='adam', loss='binary_crossentropy') SVG(model_to_dot(autoencoder,show_shapes=True).create(prog='dot', format='svg')) # In[4]: # trainX =np.expand_dims(backtorgb, 0) # trainy =np.expand_dims(colorImg, 0) # In[ ]: tensorBoardPath = '/home/yhfy2006/machineLearningInPython/AutoEncoder/logs' tb_cb = keras.callbacks.TensorBoard(log_dir=tensorBoardPath, histogram_freq=1) checkpoint = ModelCheckpoint(filepath="/home/yhfy2006/machineLearningInPython/AutoEncoder/logs/weights.hdf5", verbose=1, save_best_only=True) cbks = [checkpoint]
final_model = Sequential() final_model.add( Merge([merged_q_cd, merged_q_incd1, merged_q_incd2, merged_q_incd3], mode='concat', name='final_layer')) final_model.add(Dense(final_dense_units, init='uniform', activation='softmax')) print("Compiling the model.... ") final_model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) print("Neural Network Model Compiled Successfully!") print(final_model.summary()) from IPython.display import SVG from keras.utils.visualize_util import model_to_dot SVG( model_to_dot(final_model, show_shapes=True).create(prog='dot', format='svg')) print('Train...') final_model.fit( [x_query, x_similar, x_nonsimilar1, x_nonsimilar2, x_nonsimilar3], data_output_labels, batch_size=batch_size, nb_epoch=nb_epoch, validation_split=0.2) print('Model Fitting Completed!')
def view_svg(self): from IPython.display import SVG, display plt = SVG(self.graph.create_svg()) return display(plt)
imshow(my_image) print("class prediction vector [p(0), p(1), p(2), p(3), p(4), p(5)] = ") print(model.predict(x)) # You can also print a summary of your model by running the following code. # In[ ]: model.summary() # Finally, run the code below to visualize your ResNet50. You can also download a .png picture of your model by going to "File -> Open...-> model.png". # In[ ]: plot_model(model, to_file='model.png') SVG(model_to_dot(model).create(prog='dot', format='svg')) # ## What you should remember # - Very deep "plain" networks don't work in practice because they are hard to train due to vanishing gradients. # - The skip-connections help to address the Vanishing Gradient problem. They also make it easy for a ResNet block to learn an identity function. # - There are two main types of blocks: The identity block and the convolutional block. # - Very deep Residual Networks are built by stacking these blocks together. # ### References # # This notebook presents the ResNet algorithm due to He et al. (2015). The implementation here also took significant inspiration and follows the structure given in the GitHub repository of Francois Chollet: # # - Kaiming He, Xiangyu Zhang, Shaoqing Ren, Jian Sun - [Deep Residual Learning for Image Recognition (2015)](https://arxiv.org/abs/1512.03385) # - Francois Chollet's GitHub repository: https://github.com/fchollet/deep-learning-models/blob/master/resnet50.py #
def plotm(model): display( SVG( model_to_dot(model, show_shapes=True).create(prog='dot', format='svg')))
def viz_model_architecture(model): """Visualize model architecture in Jupyter notebook.""" display(SVG(model_to_dot(model).create(prog='dot', format='svg')))
cbow = Sequential() cbow.add( Embedding(input_dim=vocab_size, output_dim=embed_size, input_length=window_size * 2)) cbow.add(Lambda(lambda x: K.mean(x, axis=1), output_shape=(embed_size, ))) cbow.add(Dense(vocab_size, activation='softmax')) cbow.compile(loss='categorical_crossentropy', optimizer='rmsprop') print(cbow.summary()) from IPython.display import SVG from keras.utils.vis_utils import model_to_dot SVG( model_to_dot(cbow, show_shapes=True, show_layer_names=False, rankdir='TB').create(prog='dot', format='svg')) for epoch in range(1, 6): loss = 0. i = 0 for x, y in generate_context_word_pairs(corpus=wids, window_size=window_size, vocab_size=vocab_size): i += 1 loss += cbow.train_on_batch(x, y) if i % 100000 == 0: print('Processed {} (context, word) pairs'.format(i)) print('Epoch:', epoch, '\tLoss:', loss) print()