I am working on a machine learning problem involving a Monte Carlo simulation for a classification task. My current implementation involves generating synthetic class labels based on multinomial distributions and computing a specific tensor product between input features and generated labels. The goal is to estimate a regularization threshold for an ANN classification model. However, the nested loop for calculating a four-dimensional array xy seems to be a bottleneck, and I'm looking for ways to optimize this using vectorization or any other efficient approach.
Current Implementation:
Код: Выделить всё
import numpy as np import tensorflow as tf def lamb(xsample, hat_p_training, nSample=100000, miniBatchSize=500, alpha=0.05, option='quantile'): if np.mod(nSample,miniBatchSize) == 0: offset = 0 else: offset = 1 n, p1 = xsample.shape number_class = len(hat_p_training) fullList = np.zeros((miniBatchSize*(nSample//miniBatchSize+offset),)) for index in range(nSample//miniBatchSize+offset): ySample = np.random.multinomial(1, hat_p_training, size=(n, 1, miniBatchSize)) y_mean = np.mean(ySample, axis=0) xy = np.zeros(shape=(n, p1, miniBatchSize, number_class)) for index_n in np.arange(n): xy[index_n, :, :, :] = np.outer(xsample[index_n, :], (y_mean-ySample)[index_n, :, :]).reshape((p1, miniBatchSize, number_class)) xymax = np.amax(tf.reduce_sum(np.abs(tf.reduce_sum(xy, axis=0).numpy()), axis=2).numpy(), axis=0) # Further processing... - How can I optimize the calculation of xy, currently implemented with a for-loop and reshaping operations, possibly using vectorization techniques in NumPy or TensorFlow? The goal is to eliminate or reduce the for-loop for efficiency.
- Is there a more efficient way to perform these tensor operations that could leverage the capabilities of TensorFlow or NumPy for better performance?
Context:
- xsample is a 2D NumPy array of shape (n, p1), representing input features.
- hat_p_training is a 1D array representing the estimated class probabilities.
- The code generates ySample, a synthetic label set based on hat_p_training, and computes xy, a tensor representing the product between transposed xsample and the difference y_mean-ySample.
- The ultimate goal is to find the maximum value across certain dimensions of xy for further processing.
I appreciate any insights or suggestions for optimizing this part of my code. Thank you!
Источник: https://stackoverflow.com/questions/780 ... tensorflow