是否有更短的方法来导入子模块的类?

时间:2017-06-21 18:59:01

标签: python

目前,我这样做:

<div class="everything">
  <br style="line-height:15px;">
  <section class="info-boxes">
    <ul class="infobox-list">
      
      
      <li>
        <a href="javascript:void(0);">
          <div class="infobox">
            <table>
              <tr>
                <img class="profile" src="http://res.cloudinary.com/djxai1v1e/image/upload/v1497467825/Profile-Icon_oy7oxz.png"></tr>
              <tr>
                <p class="ptext">My Profile</p>
              </tr>
            </table>
            <div class="open"></div>
          </div>
          <div class="shade">
            <table>
              <tr>
                <td>
                  <img class="smallicons" src="http://res.cloudinary.com/djxai1v1e/image/upload/v1497301295/About-Me_dudglr.png">
                </td>
                <td>
                  <a href="https://northwestcomp.stage.sumtotal.host/Core/dash/profile/personalProfileManager?userid=NWTMP0001">About Me</a>
                  <hr class="hr">
                </td>
              </tr>
              <tr>
                <td><img class="smallicons" src="http://res.cloudinary.com/djxai1v1e/image/upload/v1497284615/Resume_tb7t02.png"> </td>
                  <td>
                    <a href="https://northwestcomp.stage.sumtotal.host/Core/person/resume?userid=NWTMP0001">Resume</a>
                    <hr class="hr">
                  </td>
              </tr>
              <tr>
                <td><img class="smallicons" src="http://res.cloudinary.com/djxai1v1e/image/upload/v1497280574/Accountabilities_qfdcns.png"> </td>
                  <td>
                    <a href="">Accountabilities
                    </a>
                      <hr class="hr">
                  </td>
              </tr>
              <tr>
                <td><img class="smallicons" src="http://res.cloudinary.com/djxai1v1e/image/upload/v1497970831/Chnage-Password-Icon_noszkb.png"> </td>
                  <td>
                    <a href="https://northwestcomp.stage.sumtotal.host/Core/changepassword">Change Password</a>
                    <hr class="hr">
                  </td>
              </tr>
              <tr>
                <td><img class="smallicons" src="http://res.cloudinary.com/djxai1v1e/image/upload/v1497970831/Sign-Out-Icon_twkoy7.png"> </td>
                  <td>
                    <a href="https://northwestcomp.stage.sumtotal.host/Broker/Account/Login.aspx?wtrealm=https%3a%2f%2fNORTHWESTCOMP.stage.sumtotal.host%2fcore%2f&ReturnUrl=http%3a%2f%2fnorthwestcomp.stage.sumtotal.host%2fbroker%2fToken%2fSaml11.ashx%3fwa%3dwsignin1.0%26wtrealm%3dhttps%253a%252f%252fNORTHWESTCOMP.stage.sumtotal.host%252fcore%252f%26wreply%3dhttps%253a%252f%252fnorthwestcomp.stage.sumtotal.host%252fCore%252f%252f&domainid=52160A28FC58BBBE7D714E075077AC76">Sign Out</a>
                    <hr class="hr">
                  </td>
              </tr>
            </table>
            <div class="close"></div>
          </div>
        </a>
      </li>
      </ul>
      </section>
        <div class="button_slide slide_down">
        <span class="button_slide_top">&nbsp;</span>
        myPATH User Guide</div>
   <div class="button_slidehr slide_downhr">
        <span class="button_slide_top">&nbsp;</span>
        Contact HR Now></div>
      </div>

我想做这样的事情:

import tensorflow as tf
keras = tf.contrib.keras
Sequential = keras.models.Sequential
Dense = keras.layers.Dense
Dropout = keras.layers.Dropout
Flatten = keras.layers.Flatten
Conv2D = keras.layers.Conv2D
MaxPooling2D = keras.layers.MaxPooling2D

但是当我尝试这个时,我得到了

import tensorflow as tf
keras = tf.contrib.keras
from tf.contrib.keras import (Sequential, Dense, Dropout, Flatten,
                              Conv2D, MaxPooling2D)

是否有比第一个代码块更短的方式来导入这些类?

其他尝试

ImportError: No module named tf.contrib.keras

Nr 2

>>> from tensorflow.contrib.keras import (Sequential, Dense, Dropout, Flatten)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: cannot import name Sequential

2 个答案:

答案 0 :(得分:3)

首先,您需要使用tensorflow模块的实际名称导入,而不是您用于它的tf别名:

其次,tensorflow.contrib.keras的包结构有点奇怪。名字

tensorflow.contrib.keras.models
tensorflow.contrib.keras.layers

实际上是更深层嵌套模块的别名

tensorflow.contrib.keras.api.keras.models
tensorflow.contrib.keras.api.keras.layers

以及创建这些别名的方式不允许您使用from导入来导入其内容。您可以直接使用“真实”名称:

from tensorflow.contrib.keras.api.keras.models import Sequential
from tensorflow.contrib.keras.api.keras.layers import (
        Dense, Dropout, Flatten, Conv2D, MaxPooling2D)

(这里有更多的别名 - tensorflow.contrib.keras.api.keras.*模块从tensorflow.contrib.keras.python.keras中提取大部分内容 - 但我们无需担心这一点。)

答案 1 :(得分:1)

试试这个:

from tensorflow.contrib.keras.models import Sequential
from tensorflow.contrib.keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPooling2D