{"QuestionId":50818747,"AnswerCount":0,"Tags":"","CreationDate":"2018-06-12T13:42:45.407","AcceptedAnswerId":null,"OwnerUserId":4902934.0,"Title":"How to find index of maximum value of a 2D tensor?","Body":"

I have three questions:<\/p>\n\n

    \n
  1. Is there any way to extract maximum values and its indices from a 2D tensor?<\/li>\n<\/ol>\n\n

    Suppose we have a tensor , the desired output would be . <\/p>\n\n

    I've tried the function , however the argument of doesn't accept type, for exmple: it doesn't works when , only when or . <\/p>\n\n

    I have referred to this<\/a> question but they did not answer my question above.<\/p>\n\n

      \n
    1. Also, how can I find a number of the maximum value (let's say the first 5 maximum value) of a 2D tensor, since only returns 1?<\/p><\/li>\n

    2. And after having the indices, how can I use these indices to index the value from another tensor that has the same size of above?<\/p><\/li>\n<\/ol>\n\n

      Update 1<\/strong>: As Aldream pointed out that the first two of my question is a duplicate of this question How to find the top k values in a 2-D tensor in tensorflow<\/a>. Still, the third question remains.<\/p>\n\n

      Update 2<\/strong>: For the third question, if anyone has the same problem, we can use .<\/p>\n","answers":[]} {"QuestionId":50818972,"AnswerCount":0,"Tags":"","CreationDate":"2018-06-12T13:54:43.650","AcceptedAnswerId":null,"OwnerUserId":7879074.0,"Title":"Logistic Regression classifier in Keras","Body":"

      I am training 2000 Logistic Regression classifiers using keras.\nThe inputs for each classifier are:<\/p>\n\n

      for training: vectors: 8250X50, labels:8250<\/p>\n\n

      for validation:2750X50, labels:2750<\/p>\n\n

      for testing:3000X50, labels:3000<\/p>\n\n

      for every classifier, I save the predictions and the scores (kappa score, accuracy..)<\/p>\n\n

      The code is very slow it needs three hours for training the first 600 classifiers.<\/p>\n\n

      I used the following code<\/p>\n\n

      <\/pre>\n\n

      After that I trained the 2000 classifiers as follow:<\/p>\n\n

      <\/pre>\n\n

      Is there anything that I can do to speed this process.\nI will appreciate any suggestions<\/p>\n","answers":[]} {"QuestionId":50818978,"AnswerCount":2,"Tags":"","CreationDate":"2018-06-12T13:54:57.117","AcceptedAnswerId":null,"OwnerUserId":3115675.0,"Title":"TensorFlow 1.9.0 and Python 3.6.5","Body":"

      I'm trying to upgrade to Tensorflow 1.9 within a conda environment (Ubuntu 16.04). I am using python 3.6.5. When I try this:<\/p>\n\n

      <\/pre>\n\n

      I get the error:<\/p>\n\n

      <\/pre>\n\n

      Seems strange because the same thing worked fine for TF 1.8<\/p>\n\n

      TensorFlow seems to install fine without but then when I try:<\/p>\n\n

      <\/pre>\n\n

      I get the following error:<\/p>\n\n

      <\/pre>\n\n

      I can't install from conda because it still has 1.8 when I check with:<\/p>\n\n

      <\/pre>\n","answers":[{"AnswerId":"50819043","CreationDate":"2018-06-12T13:59:02.390","ParentId":null,"OwnerUserId":"5302440","Title":null,"Body":"

      With sudo<\/code> you're installing locally. So, remove sudo -H<\/code> to install over your Environment.<\/p>\n"},{"AnswerId":"50819173","CreationDate":"2018-06-12T14:05:39.553","ParentId":null,"OwnerUserId":"7309666","Title":null,"Body":"

      Sometimes because of outdated pip also this can happen . Try this inside that environment and let me know<\/p>\n\n

      python -m pip install --upgrade pip\n<\/code><\/pre>\n\n

      Try to check if conda has 1.9 <\/p>\n\n

      conda install -c conda-forge tensorflow\n<\/code><\/pre>\n\n

      I figured out that this Tensorflow is a pre-release not the complete release, as a result you can upgrade it using pip directly.<\/p>\n\n

      You can remove your installed release and try to install this else wait for couple of weeks after that you can directly update via conda forge or pip.<\/p>\n"}]} {"QuestionId":50819035,"AnswerCount":0,"Tags":"","CreationDate":"2018-06-12T13:58:15.077","AcceptedAnswerId":null,"OwnerUserId":8165188.0,"Title":"What is the proper way to use export_savedmodel in tensorflow?","Body":"

      I am working with Tensorflow 1.8.<\/p>\n\n

      I created a custom tf.estimator , and after training I want to export my model in order to use it after for prediction. To do so, I tried ( are the inputs of my model):<\/p>\n\n

      <\/pre>\n\n

      I'm getting the following error:<\/p>\n\n

      <\/pre>\n\n

      How can I fix this ? <\/p>\n\n

      By the way, I'm not sure is properly defined with respect to .<\/p>\n","answers":[]} {"QuestionId":50819081,"AnswerCount":1,"Tags":"","CreationDate":"2018-06-12T14:00:56.847","AcceptedAnswerId":null,"OwnerUserId":8416603.0,"Title":"How can i build keras character level model using the SimpleRNN","Body":"

      I need help to build character level SimpleRNN model using the keras and tensorflow as backend. What I am trying to do is ask the model to predict the next character. Suppose 'StackOverflow' if we pass 'S' it should predict 't'. Here one word is one sequence with stop and start padding. The problem I am facing issue while training and testing the model because both the training and target data have the same shape. The code is in the gits. Any help or comments are appreciated.\nhttps:\/\/gist.github.com\/chatrapathik\/71c4dcef6cec417393f13ee4e117f9c5<\/a><\/p>\n","answers":[{"AnswerId":"50819203","CreationDate":"2018-06-12T14:06:55.837","ParentId":null,"OwnerUserId":"5825953","Title":null,"Body":"

      If you want the model to predict a single next character, You can use the training data as a target data by just shifting each character one index to the left. In python if you have numpy arrays of train_data you can do the following.<\/p>\n\n

      test_data = train_data[1:]\n<\/code><\/pre>\n\n

      Since you are not using the last characters in train data you can remove it<\/p>\n\n

      train_data = train_data[:-1]\n<\/code><\/pre>\n"}]}
      {"QuestionId":50819530,"AnswerCount":1,"Tags":"","CreationDate":"2018-06-12T14:23:36.077","AcceptedAnswerId":null,"OwnerUserId":9930882.0,"Title":"Is possible to train Tensorflow to detect defects on an object?","Body":"

      i have to say i'm totally newbie on Tensorflow. I need to train a model that using photos of the same object detect if some of them has imperfections, scratchs and so on, not detecting what object it is. Is this possible using Tensorflow? \nThanks in advance.<\/p>\n","answers":[{"AnswerId":"50854710","CreationDate":"2018-06-14T09:58:18.900","ParentId":null,"OwnerUserId":"9930882","Title":null,"Body":"

      I find the answer myself, the idea is to train a model that recognizes the defects in an image, not the object with the defect.<\/p>\n"}]} {"QuestionId":50819922,"AnswerCount":2,"Tags":"","CreationDate":"2018-06-12T14:42:41.823","AcceptedAnswerId":51424649.0,"OwnerUserId":9928923.0,"Title":"Specify either CPU or GPU for multiple models tensorflow java's job","Body":"

      I am using Tensorflow java API (1.8.0) where I load multiple models (in different sessions). Those models are loaded from .pb<\/i> files using the SavedModelBundle.load(...)<\/i> method. Those .pb<\/i> files were obtained by saving Keras' models.<\/p>\n\n

      Let's say that I want to load 3 models A, B, C.\nTo do that, I implemented a java Model<\/i> class :<\/p>\n\n

      <\/pre>\n\n

      Then I easily can instantiate 3 Model<\/i> objects corresponding to my A, B and C models with this class and make predictions with those 3 models in the same java program.\nI also noticed that if I have a GPU, the 3 models are loaded on it.<\/p>\n\n

      However, I would like only model A to be running on GPU and force the 2 others to be running on CPU.<\/strong><\/p>\n\n

      By reading documentation and diving into the source code I didn't find a way to do so. I tried to define a new ConfigProto setting visible devices to None and instantiate a new Session with the graph but it didn't work (see code below).<\/p>\n\n

      <\/pre>\n\n

      When I load the model, it uses the available GPU. Do you have any solution to this problem ?<\/p>\n\n

      Thank you for your answer.<\/p>\n","answers":[{"AnswerId":"51343688","CreationDate":"2018-07-14T21:55:58.467","ParentId":null,"OwnerUserId":"5030164","Title":null,"Body":"

      You can set the device<\/code> configuration of your tensorflow graph. Here is some relevant code [source]<\/a>. <\/p>\n\n

      ...\nbyte[] config = ConfigProto.newBuilder()\n                           .setLogDevicePlacement(true)\n                           .setAllowSoftPlacement(true)\n                           .build()\n                           .toByteArray()\n\nSession sessions[] = new Session[numModels];\n\n\/\/ function to move the graph definition to a new device \npublic static byte[] modifyGraphDef(byte[] graphDef, String device) throws Exception {\n  GraphDef.Builder builder = GraphDef.parseFrom(graphDef).toBuilder();\n  for (int i = 0; i < builder.getNodeCount(); ++i) {\n    builder.getNodeBuilder(i).setDevice(device);\n  }\n  return builder.build().toByteArray();\n} \n\ngraphA.importGraphDef(modifyGraphDef(graphDef, \"\/gpu:0\"));\ngraphB.importGraphDef(modifyGraphDef(graphDef, \"\/cpu:0\"));\n<\/code><\/pre>\n\n

      This would probably be cleaner than to do the more obvious setting of the CUDA_VISIBLE_DEVICES<\/code> environment variable to \"\"<\/code> after loading the first model.<\/p>\n"},{"AnswerId":"51424649","CreationDate":"2018-07-19T14:02:07.940","ParentId":null,"OwnerUserId":"6110786","Title":null,"Body":"

      According to this issue<\/a> , the new source code fixed this problem. Unfortunately you will have to build from source following these instructions<\/a><\/p>\n\n

      Then you can test :<\/p>\n\n

      ConfigProto configProto = ConfigProto.newBuilder()\n                .setAllowSoftPlacement(true) \/\/ allow less GPUs than configured\n                .setGpuOptions(GPUOptions.newBuilder().setPerProcessGpuMemoryFraction(0.01).build())\n                .build();\nSavedModelBundle  bundle = SavedModelBundle.loader(modelDir).withTags(\"serve\").withConfigProto(configProto.toByteArray()).load();\n<\/code><\/pre>\n"}]}
      {"QuestionId":50819931,"AnswerCount":1,"Tags":"","CreationDate":"2018-06-12T14:43:05.633","AcceptedAnswerId":50820539.0,"OwnerUserId":9805301.0,"Title":"Self-Attention GAN in Keras","Body":"

      I'm currently considering to implement the Self-Attention GAN in keras.\nThe way I'm thinking to implement is as follows:<\/p>\n\n

      <\/pre>\n\n

      But I have no idea how to add a trainable scalar gamma as described in the paper: SAGAN<\/a><\/p>\n\n

      I also hope someone can give some ideas about how to initialize a trainable keras scalar.<\/p>\n\n


      \n\n

      EDIT:<\/strong><\/p>\n\n

      My implementation is now:<\/p>\n\n

      <\/pre>\n","answers":[{"AnswerId":"50820539","CreationDate":"2018-06-12T15:14:37.810","ParentId":null,"OwnerUserId":"624547","Title":null,"Body":"

      There are several problems with the modifications you made to the original code<\/a>:<\/p>\n\n