7 examples of 'pytorch model summary' in Python

Every line of 'pytorch model summary' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
31def summary(self):
32 print(self.input_shape)
33 for l in self.layers:
34 print(l)
35 print(l.get_output_shape())
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

104def save_summary(self, summary, step, mode):
105 if mode == 'train':
106 self.train_writer.add_summary(summary, step)
107 elif mode == 'valid':
108 self.valid_writer.add_summary(summary, step)
109 self.sess.run(tf.local_variables_initializer())
261def summary(self):
262 with tf.name_scope('summaries'):
263 tf.summary.scalar('xent_loss', tf.reduce_sum(self.xent_loss))
264 tf.summary.scalar('l2_loss', tf.reduce_sum(self.lossL2))
265 tf.summary.scalar("kl_loss", tf.reduce_sum(self.kl_loss))
266 tf.summary.scalar("context_kl_loss", tf.reduce_sum(self.context_kl_loss))
267 tf.summary.scalar('total_loss', tf.reduce_sum(self.cost))
268 tf.summary.histogram("latent_vector", self.z_vector)
269 tf.summary.histogram("latent_mean", self.z_mean)
270 tf.summary.histogram("latent_log_sigma", self.z_log_sigma)
271 self.summary_op = tf.summary.merge_all()
415def __summary(self):
416 with tf.name_scope('summary'):
417 self.__mean_accuracy = tf.placeholder(tf.float32, name='mean_accuracy')
418 self.__mean_loss = tf.placeholder(tf.float32, name='mean_loss')
419 self.__mean_log_loss = tf.placeholder(tf.float32, name='mean_log_loss')
420 # self.__mean_ch_log_loss = tf.placeholder(tf.float32, name='mean_ch_log_loss')
421
422 tf.summary.scalar('learning_rate', self.__learning_rate)
423 tf.summary.scalar('mean_accuracy', self.__mean_accuracy)
424 tf.summary.scalar('mean_loss', self.__mean_loss)
425 tf.summary.scalar('mean_log_loss', self.__mean_log_loss)
103def summary(self):
104 x = torch.zeros(BATCH_SIZE, Z_DIM)
105
106 # Print the title in a good design
107 # for easy recognition.
108 print()
109 summary_title = f"| {self.__class__.__name__} Summary |"
110 for _ in range(len(summary_title)):
111 print("-", end="")
112 print()
113 print(summary_title)
114 for _ in range(len(summary_title)):
115 print("-", end="")
116 print("\n")
117
118 # Run forward pass while not tracking history on
119 # tape using `torch.no_grad()` for printing the
120 # output shape of each neural layer operation.
121 print(f"Input: {x.size()}")
122 with torch.no_grad():
123 for layer in self.main1:
124 x = layer(x)
125 print(f"Out: {x.size()} \tLayer: {layer}")
126 x = x.view(-1, 4096, 4, 4) # Reshape for convolution
127 print(f"Out: {x.size()} \tLayer: Reshape")
128 for layer in self.main2:
129 x = layer(x)
130 print(f"Out: {x.size()} \tLayer: {layer}")
64def summary(self):
65 self.adversarial.summary()
66 self.discriminator.summary()
153def log_to_tensorboard(model, step, input_var, losses, top1, topk, mode):
154 im = {'amount': 10, 'every': 3}
155
156 if step % args.tensorboard_freq == 0 or mode == 'val':
157 # (1) Log the scalar values
158 info = {
159 '{}_loss_avg'.format(mode): losses.avg,
160 '{}_loss'.format(mode): losses.val,
161
162 '{}_accr_avg'.format(mode): top1.avg,
163 '{}_accr'.format(mode): top1.val,
164
165 '{}_accr_top{}_avg'.format(mode, args.top_k): topk.avg,
166 '{}_accr_top{}'.format(mode, args.top_k): topk.val
167 }
168 for tag, value in info.items():
169 logger.scalar_summary(tag, value, step)
170
171 # (2) Log values and gradients of the parameters (histogram)
172 for tag, value in model.named_parameters():
173 tag = tag.replace('.', '/')
174 logger.histo_summary(tag, to_np(value), step)
175
176 # At the beginning it may happen
177 # source: https://discuss.pytorch.org/t/zero-grad-optimizer-or-net/1887/6
178 if value.grad is not None:
179 logger.histo_summary(tag + '/grad', to_np(value.grad), step)
180
181
182 # (3) Log the images
183 # Take non-repeating images (every=3) in specified amount
184 images = input_var.view(-1, args.image_size, args.image_size)[:(im['amount'] * im['every'])]
185 images = images[::im['every']]
186 info = {
187 '{}_images'.format(mode): to_np(images)
188 }
189 for tag, images in info.items():
190 logger.image_summary('{}_{}'.format(mode, tag), images, step)

Related snippets