Keras Tensorflow中的切片张量

时间:2019-06-06 10:01:22

标签: tensorflow keras slice tensor

例如,我有一个张量为public class Startup : FunctionsStartup { public override void Configure(IFunctionsHostBuilder builder) { // Get the path to the folder that has appsettings.json and other files. // Note that there is a better way to get this path: ExecutionContext.FunctionAppDirectory when running inside a function. But we don't have access to the ExecutionContext here. // Functions team should improve this in future. It will hopefully expose FunctionAppDirectory through some other way or env variable. string basePath = IsDevelopmentEnvironment() ? Environment.GetEnvironmentVariable("AzureWebJobsScriptRoot") : $"{Environment.GetEnvironmentVariable("HOME")}\\site\\wwwroot"; var config = new ConfigurationBuilder() .SetBasePath(basePath) .AddJsonFile("appsettings.json", optional: false, reloadOnChange: false) // common settings go here. .AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("AZURE_FUNCTIONS_ENVIRONMENT")}.json", optional: false, reloadOnChange: false) // environment specific settings go here .AddJsonFile("local.settings.json", optional: true, reloadOnChange: false) // secrets go here. This file is excluded from source control. .AddEnvironmentVariables() .Build(); builder.Services.AddSingleton<IConfiguration>(config); } public bool IsDevelopmentEnvironment() { return "Development".Equals(Environment.GetEnvironmentVariable("AZURE_FUNCTIONS_ENVIRONMENT"), StringComparison.OrdinalIgnoreCase); } } 的张量,并且我想将(None, 2, 100, 100, 1024)分为21,这样我就有2个张量为4 {{ 1}}。如何使用Keras Tensorflow做到这一点?

谢谢。

1 个答案:

答案 0 :(得分:1)

使用tf.split()

import tensorflow as tf

tensor = tf.placeholder(tf.float32, (None, 2, 100, 100, 1024))
splitted = [tf.squeeze(t, axis=1) for t in tf.split(tensor, 2, axis=1)]
print(splitted[0].get_shape().as_list(), splitted[1].get_shape().as_list())
# [None, 100, 100, 1024] [None, 100, 100, 1024]

要串联起来:

# manipulate here ...
splitted = [t[:, None, ...] for t in splitted]
res = tf.concat(splitted, axis=1)
print(res.get_shape().as_list()) # [None, 2, 100, 100, 1024]