我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用numpy.reshape()。
def model(self, features, labels): x = features["observation"] x = tf.contrib.layers.convolution2d(x, 2, kernel_size=[3, 3], stride=[2, 2], activation_fn=tf.nn.elu) x = tf.contrib.layers.convolution2d(x, 2, kernel_size=[3, 3], stride=[2, 2], activation_fn=tf.nn.elu) actions = tf.one_hot(tf.reshape(features["action"],[-1]), depth=6, on_value=1.0, off_value=0.0, axis=1) x = tf.concat(1, [tf.contrib.layers.flatten(x), actions]) x = tf.contrib.layers.fully_connected(x, 100, activation_fn=tf.nn.elu) x = tf.contrib.layers.fully_connected(x, 100, activation_fn=tf.nn.elu) logits = tf.contrib.layers.fully_connected(x, 1, activation_fn=None) prediction = tf.sigmoid(logits, name="prediction") loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits, tf.expand_dims(labels, axis=1)),name="loss") train_op = tf.contrib.layers.optimize_loss( loss, tf.contrib.framework.get_global_step(), optimizer='Adam', learning_rate=self.learning_rate) tf.add_to_collection('prediction', prediction) tf.add_to_collection('loss', loss) return prediction, loss, train_op
def visualize(self, zv, path): self.ax1.clear() self.ax2.clear() z, v = zv if path: np.save(path + '/trajectory.npy', z) z = np.reshape(z, [-1, 2]) self.ax1.hist2d(z[:, 0], z[:, 1], bins=400) self.ax1.set(xlim=self.xlim(), ylim=self.ylim()) v = np.reshape(v, [-1, 2]) self.ax2.hist2d(v[:, 0], v[:, 1], bins=400) self.ax2.set(xlim=self.xlim(), ylim=self.ylim()) if self.display: import matplotlib.pyplot as plt plt.show() plt.pause(0.1) elif path: self.fig.savefig(path + '/visualize.png')
def test(path_test, input_size, hidden_size, batch_size, save_dir, model_name, maxlen): db = read_data(path_test) X = create_sequences(db[:-maxlen], win_size=maxlen, step=maxlen) X = np.reshape(X, (X.shape[0], X.shape[1], input_size)) # build the model: 1 layer LSTM print('Build model...') model = Sequential() model.add(LSTM(hidden_size, return_sequences=False, input_shape=(maxlen, input_size))) model.add(Dense(maxlen)) model.load_weights(save_dir + model_name) model.compile(loss='mse', optimizer='adam') prediction = model.predict(X, batch_size, verbose=1) prediction = prediction.flatten() # prediction_container = np.array(prediction).flatten() Y = db[maxlen:] plt.plot(prediction, label='prediction') plt.plot(Y, label='true') plt.legend() plt.show()
def data_from_grid (cells, gridwidth, gridheight, grid=32): width = cells.shape[4] crop = (width - grid ) // 2 ## for simplicity we are assuming the same crop (and grid) in x & y directions if crop > 0: # do NOT crop with 0 as we get empty cells ... cells = cells[:,:,:,crop:-crop,crop:-crop] shape = cells.shape new_shape_1_dim = shape[0]// (gridwidth * gridheight) # ws // 36 -- Improved on 20170306 new_shape = (gridwidth * gridheight, new_shape_1_dim, ) + tuple([x for x in shape][1:]) # was 36, Improved on 20170306 cells = np.reshape(cells, new_shape) cells = np.moveaxis(cells, 0, -3) shape = cells.shape new_shape2 = tuple([x for x in shape[0:3]]) + (gridheight, gridwidth,) + tuple([x for x in shape[4:]]) cells = np.reshape(cells, new_shape2) cells = cells.swapaxes(-2, -3) shape = cells.shape combine_shape =tuple([x for x in shape[0:3]]) + (shape[-4]*shape[-3], shape[-2]*shape[-1],) cells = np.reshape(cells, combine_shape) return cells
def data_from_grid_by_proximity (cells, gridwidth, gridheight, grid=32): # disperse the sequential dats into layers and then use data_from_grid shape = cells.shape new_shape_1_dim = shape[0]// (gridwidth * gridheight) # ws // 36 -- Improved on 20170306 ### NOTE tha we invert the order of shapes below to get the required proximity type ordering new_shape = (new_shape_1_dim, gridwidth * gridheight, ) + tuple([x for x in shape][1:]) # was 36, Improved on 20170306 #new_shape = (gridwidth * gridheight, new_shape_1_dim, ) + tuple([x for x in shape][1:]) # was 36, Improved on 20170306 # swap ordering of axes cells = np.reshape(cells, new_shape) cells = cells.swapaxes(0, 1) cells = np.reshape(cells, shape) cells = data_from_grid (cells, gridwidth, gridheight, grid) return cells
def data_from_grid_by_proximity (cells, gridwidth, gridheight, grid=32): # disperse the sequential dats into layers and then use data_from_grid dspacing = gridwidth * gridheight layers = cells.shape[0] // dspacing shape = cells.shape new_shape_1_dim = shape[0]// (gridwidth * gridheight) # ws // 36 -- Improved on 20170306 ### NOTE tha we invert the order of shapes below to get the required proximity type ordering new_shape = (new_shape_1_dim, gridwidth * gridheight, ) + tuple([x for x in shape][1:]) # was 36, Improved on 20170306 # swap ordering of axes cells = np.reshape(cells, new_shape) cells = cells.swapaxes(0, 1) cells = np.reshape(cells, shape) cells = data_from_grid (cells, gridwidth, gridheight, grid) return cells
def data_from_grid (cells, gridwidth, gridheight, grid=32): #height = cells.shape[3] # should be 224 for our data width = cells.shape[4] crop = (width - grid ) // 2 ## for simplicity we are assuming the same crop (and grid) vertically and horizontally #dspacing = gridwidth * gridheight #layers = cells.shape[0] // dspacing cells = cells[:,:,:,crop:-crop,crop:-crop] shape = cells.shape new_shape_1_dim = shape[0]//36 new_shape = (36, new_shape_1_dim, ) + tuple([x for x in shape][1:]) cells = np.reshape(cells, new_shape) cells = np.moveaxis(cells, 0, -3) shape = cells.shape new_shape2 = tuple([x for x in shape[0:3]]) + (gridheight, gridwidth,) + tuple([x for x in shape[4:]]) cells = np.reshape(cells, new_shape2) cells = cells.swapaxes(-2, -3) shape = cells.shape combine_shape =tuple([x for x in shape[0:3]]) + (shape[-4]*shape[-3], shape[-2]*shape[-1],) cells = np.reshape(cells, combine_shape) return cells
def get_taxi_stats(data_path=data_path): file_name = 'taxi_data_stats.p' path = data_path + file_name if not os.path.isfile(path): download(file_name, data_path=data_path) import pickle stats = pickle.load(open(path, 'r')) sum_X = stats['sum_X'] sum_X2 = stats['sum_X2'] n = float(stats['n']) X_mean = sum_X / n X_std = ((sum_X2 - (sum_X**2)/n)/(n-1))**0.5 X_mean = np.reshape(X_mean, [1, -1]) X_std = np.reshape(X_std, [1, -1]) return X_mean, X_std
def _generateSourceData(self, format, size): if format in ('CF', 'CD'): return [complex(x) for x in xrange(size)] complexData = format.startswith('C') typecode = format[1] dataFormat, dataType = self.TYPEMAP[typecode] samples = size if complexData: samples *= 2 data = [dataType(x) for x in xrange(samples)] if complexData: data = numpy.reshape(data, (size,2)) return data
def _test_FileSource(self, format): filename = self._tempfileName('source_%s' % format) complexData = format.startswith('C') typecode = format[1] dataFormat, dataType = self.TYPEMAP[typecode] indata = self._generateSourceData(format, 16) hdr = bluefile.header(1000, format) bluefile.write(filename, hdr, indata) source = sb.FileSource(filename, midasFile=True, dataFormat=dataFormat) sink = sb.DataSink() source.connect(sink) sb.start() outdata = sink.getData(eos_block=True) if complexData: self.assertEqual(sink.sri().mode, 1) if dataFormat in ('float', 'double'): outdata = bulkio_helpers.bulkioComplexToPythonComplexList(outdata) else: outdata = numpy.reshape(outdata, (len(outdata)/2,2)) else: self.assertEqual(sink.sri().mode, 0) self.assertTrue(numpy.array_equal(indata, outdata), msg='%s != %s' % (indata, outdata))
def action_label_counts(directory, data_loader, n_actions=18, n=None): episode_paths = frame.episode_paths(directory) label_counts = [0, 0] action_label_counts = [[0, 0] for i in range(n_actions)] if n is not None: np.random.shuffle(episode_paths) episode_paths = episode_paths[:n] for episode_path in tqdm.tqdm(episode_paths): try: features, labels = data_loader.load_features_and_labels([episode_path]) except: traceback.print_exc() else: for label in range(len(label_counts)): label_counts[label] += np.count_nonzero(labels == label) for action in range(n_actions): actions = np.reshape(np.array(features["action"]), [-1]) action_label_counts[action][label] += np.count_nonzero( np.logical_and(labels == label, actions == action)) return label_counts, action_label_counts
def detect(self, img): img_h, img_w, _ = img.shape inputs = cv2.resize(img, (self.image_size, self.image_size)) inputs = cv2.cvtColor(inputs, cv2.COLOR_BGR2RGB).astype(np.float32) inputs = (inputs / 255.0) * 2.0 - 1.0 inputs = np.reshape(inputs, (1, self.image_size, self.image_size, 3)) result = self.detect_from_cvmat(inputs)[0] for i in range(len(result)): result[i][1] *= (1.0 * img_w / self.image_size) result[i][2] *= (1.0 * img_h / self.image_size) result[i][3] *= (1.0 * img_w / self.image_size) result[i][4] *= (1.0 * img_h / self.image_size) return result
def read_flow(path, filename): flowdata = None with open(path + filename + '.flo') as f: # Valid .flo file checker magic = np.fromfile(f, np.float32, count=1) if 202021.25 != magic: print 'Magic number incorrect. Invalid .flo file' else: # Reshape data into 3D array (columns, rows, bands) w = int(np.fromfile(f, np.int32, count=1)) h = int(np.fromfile(f, np.int32, count=1)) #print 'Reading {}.flo with shape: ({}, {}, 2)'.format(filename, h, w) flowdata = np.fromfile(f, np.float32, count=2*w*h) # NOTE: numpy shape(h, w, ch) is opposite to image shape(w, h, ch) flowdata = np.reshape(flowdata, (h, w, 2)) return flowdata
def create_training_test_sets(self): # training set train_x = np.random.uniform(self.data_interval_left, self.data_interval_right, size=self.data_size) train_x = np.sort(train_x) train_y = self.true_f(train_x) + 3. * np.random.randn(self.data_size) self.train_x = [train_x.reshape((train_x.shape[0], 1))] self.train_y = [train_y.reshape((train_y.shape[0], 1))] # test set for visualisation self.test_x = np.arange(self.view_xrange[0], self.view_xrange[1], 0.01, dtype=np.float32) self.test_x = np.reshape(self.test_x, (self.test_x.shape[0], 1)) self.test_y = self.true_f(self.test_x) self.test_y = np.reshape(self.test_y, (self.test_y.shape[0], 1)) self.test_x = [self.test_x] self.test_y = [self.test_y]
def _convert(matrix, arr): """Do the color space conversion. Parameters ---------- matrix : array_like The 3x3 matrix to use. arr : array_like The input array. Returns ------- out : ndarray, dtype=float The converted array. """ arr = _prepare_colorarray(arr) arr = np.swapaxes(arr, 0, -1) oldshape = arr.shape arr = np.reshape(arr, (3, -1)) out = np.dot(matrix, arr) out.shape = oldshape out = np.swapaxes(out, -1, 0) return np.ascontiguousarray(out)